Skip to content

feat(anthropic): sanitize the unsupported JSON schema validation properties#14790

Open
aayush-kapoor wants to merge 3 commits intomainfrom
aayush/anthropic-strip-schema
Open

feat(anthropic): sanitize the unsupported JSON schema validation properties#14790
aayush-kapoor wants to merge 3 commits intomainfrom
aayush/anthropic-strip-schema

Conversation

@aayush-kapoor
Copy link
Copy Markdown
Collaborator

Background

#13355 & #14342

Anthropic rejects certain schema validation properties (documented here)

When those properties are passed through for structured output definitions (not for tool definitions), Anthropic returns a 400 error.

Summary

Introduce a sanitize-json-schema.ts file that strips the rejected properties, defined in a set.
However to ensure full validation, the schema is then converted to a simple string and passed through as description()

Manual Verification

verified by running the examples:

  • examples/ai-functions/src/generate-text/anthropic/output-object-zod-positive-number.ts
  • examples/ai-functions/src/generate-text/anthropic/output-object-zod.ts

failed before the fix and worked (and respected the constraint if any) after the fix

Checklist

  • All commits are signed (PRs with unsigned commits cannot be merged)
  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • I have reviewed this pull request (self-review)

Related Issues

fixes #13355 and #14342


return descriptions.length === 0
? undefined
: `Additional constraints: ${descriptions.join('; ')}.`;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a problem in this case, but made me think if this is a form of prompt altering that we are trying to stay out of. Also using English while a the rest of the prompt might be potentially a different language. I'm sure language model can handle it, curious what you think

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.

@ai-sdk/anthropic: Unsupported JSON Schema validation properties not stripped for structured output

2 participants