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.
Param: responseFormat
The schema(s) to enforce. Can be a single Zod schema, an array of Zod schemas,
a JSON schema object, or an array of JSON schema objects.
Param: options
Optional configuration for the tool strategy
Param: options.handleError
How to handle errors when the model calls multiple structured output tools
or when the output doesn't match the schema. Defaults to true (auto-retry). Can be false (throw),
a string (retry with message), or a function (custom handler).
Param: options.toolMessageContent
Custom message content to include in conversation history
when structured output is generated via tool call
Returns
A TypedToolStrategy instance that can be used as the responseFormat in createAgent
// Multiple schemas - model can choose which one to use constagent = createAgent({ model:"claude-haiku-4-5", responseFormat:toolStrategy([ z.object({ name:z.string(), age:z.number() }), z.object({ email:z.string(), phone:z.string() }), ]), });
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,toolStrategyworks 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.
Param: responseFormat
The schema(s) to enforce. Can be a single Zod schema, an array of Zod schemas, a JSON schema object, or an array of JSON schema objects.
Param: options
Optional configuration for the tool strategy
Param: options.handleError
How to handle errors when the model calls multiple structured output tools or when the output doesn't match the schema. Defaults to
true(auto-retry). Can befalse(throw), astring(retry with message), or afunction(custom handler).Param: options.toolMessageContent
Custom message content to include in conversation history when structured output is generated via tool call
Returns
A
TypedToolStrategyinstance that can be used as theresponseFormatincreateAgentExample
Example