Creates a provider strategy for structured output using native JSON schema support.
This function is used to configure structured output for agents when the underlying model
supports native JSON schema output (e.g., OpenAI's gpt-4o, gpt-4o-mini, and newer models).
Unlike toolStrategy, which uses function calling to extract structured output, providerStrategy
leverages the provider's native structured output capabilities, resulting in more efficient
and reliable schema enforcement.
When used with a model that supports JSON schema output, the model will return responses that directly conform to the provided schema without requiring tool calls. This is the recommended approach for structured output when your model supports it.
providerStrategy<
T extends InteropZodType<unknown>
>(responseFormat: T | __type): ProviderStrategy<T extends InteropZodType<U> U : never>| Name | Type | Description |
|---|---|---|
responseFormat* | T | __type | The schema to enforce, either a Zod schema, a JSON schema object, or an options object with |
import { providerStrategy, createAgent } from "langchain";
import { z } from "zod";
const agent = createAgent({
model: "claude-haiku-4-5",
responseFormat: providerStrategy(
z.object({
answer: z.string().describe("The answer to the question"),
confidence: z.number().min(0).max(1),
})
),
});// Using strict mode for stricter schema enforcement
const agent = createAgent({
model: "claude-haiku-4-5",
responseFormat: providerStrategy({
schema: z.object({
name: z.string(),
age: z.number(),
}),
strict: true
}),
});