Mistral
Wrap your Mistral client with Shield to harden prompts, detect injection in user messages and tool results, and guard output on chat.complete and chat.stream.
shieldMistral wraps your Mistral client from @mistralai/mistralai and intercepts every chat.complete and chat.stream call.
Usage
import { Mistral } from "@mistralai/mistralai";
import { shieldMistral } from "@zeroleaks/shield/mistral";
const client = shieldMistral(
new Mistral({ apiKey: process.env.MISTRAL_API_KEY }),
{ systemPrompt: "You are a support agent..." }
);
const response = await client.chat.complete({
model: "mistral-large-latest",
messages: [
{ role: "system", content: "You are a support agent..." },
{ role: "user", content: userInput },
],
});What Shield checks
Shield applies the shared behavior to these fields:
| Step | Fields |
|---|---|
| Harden | Every message with role: "system". String content stays a string. In an array of chunks, Shield joins the text chunks with newlines, hardens them, and stores the combined text in the first text chunk. Thinking chunks keep their positions. |
| Detect | Every user message and every tool message. Text chunks are joined; image, document, file, and audio chunks are ignored. Assistant messages are not scanned. |
| Guard | For every choice, the text of message and of each entry in messages: string content, or its text chunks joined. Chunked text is written back over the same chunks: each keeps its original character count, and the last chunk receives the remaining text. Thinking chunks are unchanged. Each tool call's function.arguments is also guarded, as one text when it is a JSON string and string by string when it is an object. |
The wrapper uses the SDK's camelCase field names: toolCalls, toolCallId, and finishReason.
Options
shieldMistral accepts the shared options. One row differs:
| Option | Type | Default | Description |
|---|---|---|---|
systemPrompt | string | the first system message | The system prompt to check output against. Defaults to the text of the first system message, with its text chunks joined with newlines, before hardening. |
Streaming
chat.stream() resolves to the SDK's EventStream, whose events each carry a completion chunk in data. In "buffer" and "chunked" mode, the wrapper resolves to a stream of the same kind: a ReadableStream with the EventStream prototype, so both for await and getReader() work. It follows the shared modes like this:
| Mode | Behavior |
|---|---|
"buffer" | The promise resolves once the whole stream has been read, and rejects if the stream fails or a leak or finding throws. Each choice's text across all events (delta.content, a string or text chunks) is guarded as one text and written back over the same deltas, each keeping its length and the last one taking the rest. Each tool call's argument strings, matched by index, are guarded the same way; object arguments are guarded delta by delta. You get every event the SDK sent, as the same objects and in order, with roles, finish reasons, and usage. |
"chunked" | Each choice's text is guarded every streamingChunkSize characters. Every event is kept, with its delta.content holding the text that is safe to send so far, which can be empty. Events are yielded one behind the stream, so the text still held back at the end goes into the last event, ahead of its finish reason and usage. Tool call arguments are guarded delta by delta, since Mistral sends a call's arguments in one delta. |
Nothing is dropped and every choice is guarded. "passthrough" returns the SDK's stream unchanged. With throwOnLeak or blockOnOutputFindings, a finding in tool call arguments throws as soon as its event arrives. Findings in text throw as described under Chunk boundaries.
Limitations
chat.parse()andchat.parseStream()work on the wrapped client but are not guarded, because the SDK sends those requests itself instead of throughcompleteandstream. For guarded structured output, callchat.completewithresponseFormatand parse the JSON yourself.- Other resources keep working but are not guarded, such as
client.agents.complete()andclient.agents.stream(),client.beta.conversations, andclient.fim. - Thinking chunks are not read, in requests or responses. Image, document, file, and audio chunks are not fetched or decoded.
See Limitations for what no wrapper covers.