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
6 changes: 5 additions & 1 deletion openhands-sdk/openhands/sdk/agent/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ def fix_malformed_tool_arguments(

origin = get_origin(expected_type)
if origin is Union or origin is types.UnionType:
type_args = get_args(expected_type)
# Unwrap Annotated members too, e.g. Annotated[list[int], ...] | None
type_args = [
get_args(arg)[0] if get_origin(arg) is Annotated else arg
for arg in get_args(expected_type)
]
expected_origins = [get_origin(arg) or arg for arg in type_args]
else:
expected_origins = [origin or expected_type]
Expand Down
15 changes: 15 additions & 0 deletions tests/sdk/agent/test_fix_malformed_tool_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class StrOrListAction(Action):
value: str | list[str] = Field(description="A str-or-list field")


class OptionalAnnotatedAction(Action):
"""Optional fields whose inner type carries ``Annotated`` metadata."""

items: Annotated[list[int], Field(description="Optional list")] | None = None
text: Annotated[str, Field(description="Optional string")] | None = None


class _NestedActionForMalformedArgs(Action):
"""Action with nested structures for testing JSON decoding.

Expand Down Expand Up @@ -383,3 +390,11 @@ def test_str_or_list_field_list_passthrough():
action = StrOrListAction.model_validate(fixed_data)

assert action.value == ["a", "b", "c"]


def test_optional_annotated_fields_are_repaired():
data = {"items": "[1, 2]", "text": ["a", "b"]}

fixed = fix_malformed_tool_arguments(data, OptionalAnnotatedAction)

assert fixed == {"items": [1, 2], "text": "ab"}
1 change: 0 additions & 1 deletion tests/sdk/conversation/test_local_conversation_mcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,6 @@ def _failed_servers(caplog: pytest.LogCaptureFixture) -> list[str]:
"broken",
[
pytest.param(UNRESPONSIVE_SERVER, id="never-answers"),
pytest.param({}, id="invalid-entry"),
pytest.param({"command": "missing-mcp-server-command"}, id="missing-command"),
],
)
Expand Down
Loading