mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-06 02:48:13 +00:00
fix: update descriptions with removed constraint info in filter_anthropic_output_schema
Address review feedback: the function now appends removed constraint information to the description field (matching Anthropic SDK behavior), rather than silently dropping constraints.
This commit is contained in:
parent
c4f4735fd4
commit
6b5c2728cb
2 changed files with 44 additions and 1 deletions
|
|
@ -240,10 +240,41 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
|
|||
"minLength", "maxLength", # string constraints
|
||||
}
|
||||
|
||||
# Build description additions from removed constraints
|
||||
constraint_descriptions: list = []
|
||||
constraint_labels = {
|
||||
"minItems": "minimum number of items: {}",
|
||||
"maxItems": "maximum number of items: {}",
|
||||
"minimum": "minimum value: {}",
|
||||
"maximum": "maximum value: {}",
|
||||
"exclusiveMinimum": "exclusive minimum value: {}",
|
||||
"exclusiveMaximum": "exclusive maximum value: {}",
|
||||
"minLength": "minimum length: {}",
|
||||
"maxLength": "maximum length: {}",
|
||||
}
|
||||
for field in unsupported_fields:
|
||||
if field in schema:
|
||||
constraint_descriptions.append(
|
||||
constraint_labels[field].format(schema[field])
|
||||
)
|
||||
|
||||
result: Dict[str, Any] = {}
|
||||
|
||||
# Update description with removed constraint info
|
||||
if constraint_descriptions:
|
||||
existing_desc = schema.get("description", "")
|
||||
constraint_note = "Note: " + ", ".join(constraint_descriptions) + "."
|
||||
if existing_desc:
|
||||
result["description"] = existing_desc + " " + constraint_note
|
||||
else:
|
||||
result["description"] = constraint_note
|
||||
|
||||
for key, value in schema.items():
|
||||
if key in unsupported_fields:
|
||||
continue
|
||||
if key == "description" and "description" in result:
|
||||
# Already handled above
|
||||
continue
|
||||
|
||||
if key == "properties" and isinstance(value, dict):
|
||||
result[key] = {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,13 @@ class TestFilterAnthropicOutputSchema:
|
|||
|
||||
# Other fields preserved
|
||||
assert result["properties"]["age"]["type"] == "integer"
|
||||
assert result["properties"]["age"]["description"] == "Person's age"
|
||||
# Description should be updated with removed constraint info
|
||||
assert "Person's age" in result["properties"]["age"]["description"]
|
||||
assert "minimum value: 0" in result["properties"]["age"]["description"]
|
||||
assert "maximum value: 150" in result["properties"]["age"]["description"]
|
||||
# Score had no description, should get one from constraints
|
||||
assert "exclusive minimum value: 0" in result["properties"]["score"]["description"]
|
||||
assert "exclusive maximum value: 100" in result["properties"]["score"]["description"]
|
||||
|
||||
def test_removes_string_constraints(self):
|
||||
"""Test that minLength/maxLength are removed from string schemas."""
|
||||
|
|
@ -60,6 +66,9 @@ class TestFilterAnthropicOutputSchema:
|
|||
assert "minLength" not in result["properties"]["name"]
|
||||
assert "maxLength" not in result["properties"]["name"]
|
||||
assert result["properties"]["name"]["type"] == "string"
|
||||
# Description should contain constraint info
|
||||
assert "minimum length: 1" in result["properties"]["name"]["description"]
|
||||
assert "maximum length: 100" in result["properties"]["name"]["description"]
|
||||
|
||||
def test_removes_array_constraints(self):
|
||||
"""Test that minItems/maxItems are removed from array schemas."""
|
||||
|
|
@ -76,6 +85,9 @@ class TestFilterAnthropicOutputSchema:
|
|||
assert "maxItems" not in result
|
||||
assert result["type"] == "array"
|
||||
assert result["items"] == {"type": "string"}
|
||||
# Description should contain constraint info
|
||||
assert "minimum number of items: 1" in result["description"]
|
||||
assert "maximum number of items: 10" in result["description"]
|
||||
|
||||
def test_handles_nested_schemas(self):
|
||||
"""Test that nested schemas are also filtered."""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue