Strip incompatible JSON Schema keywords from tool schemas

This commit is contained in:
skfallin
2026-04-02 13:50:47 +02:00
parent 5cd95f4bb1
commit 0c88dea247
4 changed files with 104 additions and 3 deletions

View File

@@ -0,0 +1,30 @@
/**
* Anthropic-compatible tool schemas reject several JSON Schema keywords that
* Zod commonly emits, especially string `format` validators like `uri`.
* Strip those fields recursively before sending tool schemas to providers.
*/
export function stripIncompatibleSchemaKeywords<T>(
schema: T,
): T {
if (Array.isArray(schema)) {
return schema.map(item => stripIncompatibleSchemaKeywords(item)) as T
}
if (!schema || typeof schema !== 'object') {
return schema
}
const result: Record<string, unknown> = {}
for (const [key, value] of Object.entries(schema as Record<string, unknown>)) {
if (key === '$schema' || key === 'format' || key === 'propertyNames') {
continue
}
result[key] =
value && typeof value === 'object'
? stripIncompatibleSchemaKeywords(value)
: value
}
return result as T
}