Creates a tool strategy for structured output using function calling.
This function configures structured output by converting schemas into function tools that
the model calls. Unlike providerStrategy, which uses native JSON schema support,
toolStrategy works with any model that supports function calling, making it more
widely compatible across providers and model versions.
The model will call a function with arguments matching your schema, and the agent will extract and validate the structured output from the tool call. This approach is automatically used when your model doesn't support native JSON schema output.
toolStrategy<
T extends InteropZodType<any>
>(
responseFormat: T,
options: ToolStrategyOptions
): TypedToolStrategy<T extends InteropZodType<U> U : never>| Name | Type | Description |
|---|---|---|
responseFormat* | T | |
options | ToolStrategyOptions |
import { toolStrategy, createAgent } from "langchain";
import { z } from "zod";
const agent = createAgent({
model: "claude-haiku-4-5",
responseFormat: toolStrategy(
z.object({
answer: z.string(),
confidence: z.number().min(0).max(1),
})
),
});// Multiple schemas - model can choose which one to use
const agent = createAgent({
model: "claude-haiku-4-5",
responseFormat: toolStrategy([
z.object({ name: z.string(), age: z.number() }),
z.object({ email: z.string(), phone: z.string() }),
]),
});