Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/agents/strict_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ def _ensure_strict_json_schema(
if json_schema.get("default", NOT_GIVEN) is None:
json_schema.pop("default")

# Remove all null values to comply with JSON Schema Draft 2020-12
# This prevents LLM providers from rejecting schemas with null values
keys_to_remove = []
for key, value in json_schema.items():
if value is None:
keys_to_remove.append(key)

for key in keys_to_remove:
json_schema.pop(key)
Comment on lines +112 to +120

Choose a reason for hiding this comment

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

[P1] Preserve strictness when additionalProperties is None

The new loop removes every key whose value is None. For object schemas that arrive with "additionalProperties": None, the earlier branch that defaults objects to additionalProperties=False is skipped because the key is present. After this block runs, the key is popped entirely, so the returned schema has no additionalProperties entry and therefore permits extra properties by default. This regresses the purpose of ensure_strict_json_schema, which should always disallow additional fields. Consider treating None as “not provided” before the object-type check or coercing it to False rather than dropping the key.

Useful? React with 👍 / 👎.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch


# we can't use `$ref`s if there are also other properties defined, e.g.
# `{"$ref": "...", "description": "my description"}`
#
Expand Down
32 changes: 32 additions & 0 deletions tests/test_strict_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,35 @@ def test_invalid_ref_format():
schema = {"type": "object", "properties": {"a": {"$ref": "invalid", "description": "desc"}}}
with pytest.raises(ValueError):
ensure_strict_json_schema(schema)


def test_null_values_removal():
"""
Test that null values are removed from JSON schemas to comply with JSON Schema Draft 2020-12.
"""
schema_with_nulls = {
"type": "object",
"properties": {
"param": {
"type": "boolean",
"default": True,
"enum": None,
"minimum": None,
"maximum": None,
"items": None,
"properties": None,
}
},
}

result = ensure_strict_json_schema(schema_with_nulls)

# The result should not contain any null values
param_schema = result["properties"]["param"]
assert param_schema["type"] == "boolean"
assert param_schema["default"] is True
assert "enum" not in param_schema
assert "minimum" not in param_schema
assert "maximum" not in param_schema
assert "items" not in param_schema
assert "properties" not in param_schema