Repository navigation
sdk: make Zod the source of truth for agent tool schemas - #81
Draft
thorstenhans wants to merge 1 commit into
Draft
thorstenhans wants to merge 1 commit into
thorstenhans wants to merge 1 commit into
Conversation
interpreterTool() now returns a strict Zod schema as inputSchema, which the Vercel AI SDK consumes directly. toAnthropicTool(), toOpenAITool(), and the new toToolDefinition() derive JSON Schema from it with z.toJSONSchema; hand-written JSON Schema ToolDefinitions are still accepted. - The derived schema and description are identical to the previous hand-written output for every language configuration, pinned by a golden fixture test. - The input type narrows to the configured languages (no `dependencies` for Python-only tools). - inputSchema.safeParse() rejects unknown fields such as allowedHosts, replacing hand-rolled argument checks in the ai-code-executor example. - README documents Interpreter + tool setup for the Vercel AI SDK ToolLoopAgent and the Anthropic SDK. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Frameworks like the Vercel AI SDK define tools with Zod. Turning the SDK's hand-written JSON Schema (the Anthropic / OpenAI shape) into something those frameworks understand is error-prone and loses validation and types. Going the other way is reliable: Zod 4 generates JSON Schema itself (
z.toJSONSchema).What changes
interpreterTool()returns a strict Zod schema asinputSchema.{ description, inputSchema }goes straight into the AI SDK'stool(), andexecutereceives typed arguments.toAnthropicTool(),toOpenAITool(), and the newtoToolDefinition(). They still accept hand-written JSON SchemaToolDefinitions (app-builder'screate_appis unchanged).inputSchema.safeParse(args)rejects unknown fields such asallowedHosts,kvEnabled, andidentity, so the model cannot set execution policy. The ai-code-executor example uses it instead of hand-rolled checks.interpreterTool({ languages: ["python"] })inferslanguage: "python"with nodependencies, matching the schema.ToolLoopAgentand the Anthropic SDK, with the floating-promise rule in the agent system prompt (per AGENTS.md).zod@^4.6.7is a regular dependency, imported only by the./toolsentry point.Compatibility
What the model sees is unchanged. For every language configuration, the derived JSON Schema and the description are identical to the previous hand-written output (key order aside). A golden fixture (
sdk/test/fixtures/interpreter-tool.json) pins this. While building it, the comparison caught two regressions, both fixed here:input.bodyhad become required.propertyNames: { type: "string" }for string maps; this is now stripped because OpenAI strict mode rejects it.Breaking (0.1.0):
interpreterTool().inputSchemais now a Zod schema. Code that reads JSON Schema from it should usetoToolDefinition(tool).inputSchema.Testing
sdk:npm run check(typecheck incl. new compile-time type assertions; 45 tests pass, including the golden test, which I confirmed fails when thebodyregression is put back)ai@7.0.166,@ai-sdk/anthropic@4.0.99, and@anthropic-ai/sdk@1.6.0(scratch project, not run against a model)ai-code-executorandapp-builder(both providers) andsdk-componentbuild to Wasm; example lockfiles only gain the SDK'szodentry and passnpm cinpm auditgate:sdkis clean. The gate still fails forexamples/ai-code-executor/uiandexamples/app-builder/ui, which this PR does not touchcheck-imports.mjs,smoke.hurl,run-examples.sh). They cover the sandbox components, which do not depend on the SDKOpen questions for review
zodas a regular dependency or a peer dependency (a peer dependency avoids two copies of zod in apps that also use it, but users must install it themselves)🤖 Generated with Claude Code