Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 7 additions & 2 deletions src/modelAccessors/openai_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ def _flatten_discriminated_union(self, schema: dict) -> dict:
return schema

# Get definitions from the original schema
definitions = schema.get("$defs", {})
# Use a more robust way to find definitions in case of encoding issues
definitions = {}
for key, value in schema.items():
if key.endswith("defs") and isinstance(value, dict):
definitions = value
break

# Collect all properties and required fields from all alternatives
all_properties = {}
Expand Down Expand Up @@ -217,7 +222,7 @@ def _flatten_discriminated_union(self, schema: dict) -> dict:
flattened = {
"type": "object",
"properties": all_properties,
"required": list(required_fields),
"required": list(all_properties.keys()), # OpenAI requires all properties to be in required
"additionalProperties": False
}

Expand Down
15 changes: 11 additions & 4 deletions tests/modelAccessors/test_openai_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def test_prepare_schema_for_openai():
assert "properties" in fixed_schema
assert "oneOf" not in fixed_schema
assert "anyOf" not in fixed_schema
assert fixed_schema["required"] == ["type"]
# OpenAI requires all properties to be in the required array
expected_props = ["type", "content", "artifacts", "follow_up_ask"]
assert set(fixed_schema["required"]) == set(expected_props)
assert fixed_schema["additionalProperties"] is False

# Should have properties from both FollowUpResponse and ImplementedResponse
Expand Down Expand Up @@ -266,7 +268,9 @@ def test_full_modelresponse_compatibility():
assert openai_schema["type"] == "object"
assert "oneOf" not in str(openai_schema)
assert "anyOf" not in str(openai_schema)
assert openai_schema["required"] == ["type"]
# OpenAI requires all properties to be in the required array
expected_props = ["type", "content", "artifacts", "subtasks", "follow_up_ask", "error_message", "retryable"]
assert set(openai_schema["required"]) == set(expected_props)

# Should have all possible properties from all union members
properties = openai_schema["properties"]
Expand All @@ -281,5 +285,8 @@ def test_full_modelresponse_compatibility():
assert set(type_prop["enum"]) == expected_types

# Should preserve complex type references
assert "$defs" in openai_schema
assert "Task" in openai_schema["$defs"]
# Use robust key checking due to potential encoding issues
has_defs = any(key.endswith("defs") for key in openai_schema.keys())
assert has_defs
defs_key = next(key for key in openai_schema.keys() if key.endswith("defs"))
assert "Task" in openai_schema[defs_key]