Skip to content

Commit dbe8e09

Browse files
sammyawadclaude
andcommitted
fix(schema): raise instead of silently dropping invalid local model instances
skip_invalid_items (x-deserialize-skip-invalid-items, #117) drops array items that fail validation. That is right for wire data from a newer peer, but it also dropped model instances built in local code: NewSessionRequest(mcp_servers=[McpServerHttp(...)]) became an empty list with no error, so the agent started without its MCP server. An item that is already a BaseModel instance now re-raises its ValidationError. Dicts from the wire keep today's lenient behaviour. Fixes #203 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0459667 commit dbe8e09

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

‎src/acp/_deserialize.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from collections.abc import Callable
1212
from typing import Any
1313

14-
from pydantic import ValidationError
14+
from pydantic import BaseModel, ValidationError
1515
from pydantic_core import PydanticUseDefault
1616

1717

@@ -50,6 +50,11 @@ def skip_invalid_items(value: Any, handler: Callable[[Any], Any], _info: Any = N
5050
5151
Restores ``x-deserialize-skip-invalid-items``. Each item is validated through the field's
5252
own list handler, so item coercion and salvaging still apply to the survivors.
53+
54+
Only wire data is skipped. An item that is already a model instance was built in local
55+
code, so a validation failure there is a programming error (for example passing
56+
``McpServerHttp`` where the tagged ``HttpMcpServer`` is expected) and is raised instead of
57+
being dropped silently.
5358
"""
5459
if not isinstance(value, list):
5560
return handler(value)
@@ -58,5 +63,7 @@ def skip_invalid_items(value: Any, handler: Callable[[Any], Any], _info: Any = N
5863
try:
5964
salvaged.append(handler([item])[0])
6065
except ValidationError:
66+
if isinstance(item, BaseModel):
67+
raise
6168
continue
6269
return salvaged

‎tests/test_deserialize.py‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66

77
from typing import Any
88

9-
from pydantic import BaseModel, field_validator
9+
import pytest
10+
from pydantic import BaseModel, ValidationError, field_validator
1011

1112
from acp._deserialize import salvage_on_error, skip_invalid_items
12-
from acp.schema import ReadTextFileRequest, ToolCallUpdate, WriteTextFileRequest
13+
from acp.schema import (
14+
HttpHeader,
15+
HttpMcpServer,
16+
McpServerHttp,
17+
NewSessionRequest,
18+
ReadTextFileRequest,
19+
ToolCallUpdate,
20+
WriteTextFileRequest,
21+
)
1322

1423

1524
class _Salvage(BaseModel):
@@ -60,3 +69,46 @@ def test_skip_invalid_items_on_generated_model() -> None:
6069
good = {"type": "content", "content": {"type": "text", "text": "ok"}}
6170
update = ToolCallUpdate.model_validate({"toolCallId": "t", "content": [good, {"bogus": 1}]})
6271
assert len(update.content or []) == 1
72+
73+
74+
class _Item(BaseModel):
75+
n: int
76+
77+
78+
class _Other(BaseModel):
79+
s: str
80+
81+
82+
class _SkipModels(BaseModel):
83+
xs: list[_Item] = []
84+
85+
@field_validator("xs", mode="wrap")
86+
@classmethod
87+
def _v(cls, value: Any, handler: Any) -> Any:
88+
return skip_invalid_items(value, handler)
89+
90+
91+
def test_skip_invalid_items_raises_for_local_model_instances() -> None:
92+
# Wire data (dicts) stays lenient...
93+
assert _SkipModels.model_validate({"xs": [{"n": 1}, {"bogus": 1}]}).xs == [_Item(n=1)]
94+
# ...but a wrong model built in code is a programming error, not data from a newer peer.
95+
with pytest.raises(ValidationError):
96+
_SkipModels(xs=[_Item(n=1), _Other(s="x")]) # type: ignore[list-item]
97+
98+
99+
def test_new_session_request_rejects_untagged_mcp_server_instance() -> None:
100+
headers = [HttpHeader(name="Authorization", value="Bearer x")]
101+
untagged = McpServerHttp(name="m", url="http://127.0.0.1:1/mcp", headers=headers)
102+
with pytest.raises(ValidationError):
103+
NewSessionRequest(cwd="/w", mcp_servers=[untagged]) # type: ignore[list-item]
104+
ok = NewSessionRequest(
105+
cwd="/w", mcp_servers=[HttpMcpServer(name="m", url="http://127.0.0.1:1/mcp", headers=headers)]
106+
)
107+
assert [s.name for s in ok.mcp_servers] == ["m"]
108+
# Incoming JSON is unchanged: the same fields as a dict parse (type defaults to "http"),
109+
# and a malformed server is still skipped.
110+
wire = NewSessionRequest.model_validate({
111+
"cwd": "/w",
112+
"mcpServers": [{"name": "m", "url": "u", "headers": []}, {"bogus": 1}],
113+
})
114+
assert [s.name for s in wire.mcp_servers] == ["m"]

0 commit comments

Comments
 (0)