Overview
ThedefineTool() function defines tools (functions) that language models can call during generation. Tools enable models to access external data, perform calculations, or trigger actions beyond text generation.
Function signature
Parameters
string
required
The unique name of the tool. This is what the model uses to identify and
call the tool. Should be descriptive and follow snake_case convention (e.g.,
get_weather, search_database).string
required
A clear description of what the tool does. This helps the model understand
when and how to use it. Should explain the tool’s purpose, expected inputs,
and what it returns.
SchemaInput
required
Schema of the tool’s input parameters: a Standard Schema with JSON Schema
support, such as a Zod 4.2+ schema, or plain JSON Schema. The model
generates arguments matching this schema. Check calls to tools with Standard
Schema parameters with
validateToolCall().boolean
Opts this tool into provider-enforced adherence to its parameter schema.
Omitted or
false keeps the tool non-strict on every provider.Return value
Returns the sameToolDefinition object that was passed in. This is a type-safe helper that provides IDE autocompletion and validation.
Strict schema enforcement
strict: true opts a single tool into provider-enforced schema adherence. The
tool’s schema must fit the strict-capable subset (closed objects whose keys
are required, nullable, or defaulted — in Zod, use .nullable() instead of a
bare .optional()), which core-ai validates locally before the provider
request. Check
model.capabilities.tools.strictSchemas before opting tools in.
Strict enforcement does not replace runtime validation. Check each call with
validateToolCall() before executing the tool. See
Strict tool schemas in the
tool-calling guide for the schema contract, error reasons, and provider
support.
validateToolCall()
Validates the arguments of a tool call with the tool’s own Standard Schema.parameters, it returns the schema’s result, typed with
the schema’s output:
{ value }when the arguments match.valueis the schema’s output, with defaults and transforms applied.{ issues }when they do not. Each issue has amessageand an optionalpath. A validator that throws, such as a transform callingnew URL(), is reported as an issue too.
parameters, such as tool schemas from an MCP server, it
returns undefined: core-ai ships no JSON Schema validator, so validate those
arguments yourself.
It throws a ValidationError when toolCall is a call to another tool, or
when the tool’s parameters are not a usable schema, which a request with the
tool rejects too.
Send issues back to the model as an error tool result, and run the tool with
value, not with the raw ToolCall.arguments. See
Validate tool calls for complete
examples.