Function Signature
Parameters
An instance of the Groq SDK client (from
groq-sdk package >= 0.3.0)Configuration options for Shield protection
ShieldGroqOptions
System prompt used for sanitization. When omitted, Shield automatically derives it from the first system message in your request.
Options for prompt hardening. Set to
false to disable hardening. See harden() for available options.Options for injection detection. Set to
false to disable detection. See detect() for available options.Options for output sanitization. Set to
false to disable sanitization. See sanitize() for available options.Streaming sanitization strategy:
"buffer": Accumulate the full stream, then sanitize (higher memory, more accurate)"chunked": Process in 8KB chunks (lower memory for long streams)"passthrough": Skip sanitization entirely (use when you accept the risk)
Chunk size in bytes for
"chunked" mode. Only applies when streamingSanitize is set to "chunked".Behavior when injection is detected:
"block": ThrowInjectionDetectedError(request fails)"warn": Only invokeonInjectionDetectedcallback (request continues)
When
true, throw LeakDetectedError instead of redacting leaked content. Use for strict security policies where any leak should abort the request.Callback invoked when an injection is detected. Receives the full
DetectResult with risk level and matched patterns.Callback invoked when a prompt leak is detected in the output. Receives the full
SanitizeResult with confidence score and leaked fragments.Return Type
Returns the same client typeT with Shield protection applied. All methods work identically to the original client.
Examples
Basic Usage
Streaming with Chunked Sanitization
Custom Detection Callbacks
Strict Mode (Throw on Any Leak)
Notes
- OpenAI-compatible API: Groq uses the same API format as OpenAI, so the usage patterns are identical.
- Multi-part messages: Groq supports
contentasstring | ContentPart[](e.g., text + images). Shield extracts text from all parts for injection detection and hardening. - Tool calls: Shield automatically sanitizes function arguments in tool calls to prevent leaks in structured outputs.
- Auto-derived system prompt: When
systemPromptis not provided, Shield extracts it from the first system message in your request.