Skip to content

Fix Anthropic native structured output rejecting min/max and other JSON Schema constraints#718

Open
sulimanbenhalim wants to merge 1 commit into
laravel:0.xfrom
sulimanbenhalim:fix/anthropic-structured-output-schema-sanitization
Open

Fix Anthropic native structured output rejecting min/max and other JSON Schema constraints#718
sulimanbenhalim wants to merge 1 commit into
laravel:0.xfrom
sulimanbenhalim:fix/anthropic-structured-output-schema-sanitization

Conversation

@sulimanbenhalim

Copy link
Copy Markdown
Contributor

Fixes #715

When an agent's schema() uses Laravel's JsonSchema builder methods like ->min() or ->max(), Anthropic native structured output returns a 400. Its output_config.format.schema only accepts a subset of JSON Schema, so minimum, maximum, minLength, maxLength, maxItems, multipleOf and uniqueItems get rejected before the model even runs. The same schema works on OpenAI.

The constraints reach the API unfiltered because ObjectSchema::toSchema() only disables additionalProperties and Illuminate's Serializer keeps everything else.

This adds AnthropicSchemaSanitizer, which strips the unsupported keywords on the native path and folds each one into the node's description so the model still honors it, the same transform Anthropic's own Python, TS, Ruby and PHP SDKs do:

$schema->integer()->required()->min(1)->max(10);
// sent as { "type": "integer", "description": "Must be at least 1. Must be at most 10." }

minItems > 1 is clamped to 1 (Anthropic allows only 0 or 1), pattern is left alone since simple patterns are supported, and the synthetic-tool fallback isn't touched since tool input_schema takes these as hints.

Tests cover every stripped keyword plus an integration test checking the constraints are gone from the request body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic native structured output rejects JSON Schema validation keywords (min/max, minLength, minItems, …) with a 400

1 participant