fix(lib): support JSON Schema type arrays in transform_schema (Closes #1876) - #1892
fix(lib): support JSON Schema type arrays in transform_schema (Closes #1876)#1892Synxneuos wants to merge 2 commits into
Conversation
sylvesterkaczmarek
left a comment
There was a problem hiding this comment.
Converting a type array from only the type names drops type-specific sibling keywords. For example {'type':['array','null'],'items':{'type':'string'}} becomes an anyOf whose array branch has no items; the original items is only appended to the description, so item type is no longer enforced. The same applies to other type-specific keywords. Could each generated branch be transformed with the applicable sibling schema keywords, or otherwise preserve those constraints?
… arrays into anyOf
|
Thanks for the thorough review @sylvesterkaczmarek! Updated in commit We now extract and distribute type-specific sibling keywords ( For example, {
"anyOf": [
{
"type": "array",
"items": {
"type": "string"
}
},
{
"type": "null"
}
],
"description": "Optional list"
}Added unit tests covering type arrays with |
Summary
Closes #1876
When a JSON Schema specifies
typeas an array (e.g.,{"type": ["string", "null"]}as generated by PydanticOptional[T]or Zod.nullable()),transform_schema()previously raised anAssertionErrordue to unmatched type branches in_transform.py.Changes
src/anthropic/lib/_parse/_transform.py:transform_schema(), mappingtype: [T1, T2]toanyOf: [{"type": T1}, {"type": T2}].type_toOptional[SupportedTypes | list[SupportedTypes]].is_list(type_)in the terminal validation pass to prevent falseassert_nevertriggers.tests/lib/_parse/test_transform.py:test_type_array()andtest_type_array_in_object().Verification
tests/lib/_parse/test_transform.pypassed (16 passed in 6.03s).