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
8 changes: 6 additions & 2 deletions src/ai/types/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ class MessageBundle(pydantic.BaseModel):
)


_MODEL_INPUT_UNSET: Any = object()
class _ModelInputUnset:
pass


_MODEL_INPUT_UNSET: Any = _ModelInputUnset()

# Coarse tag for the shape of ``ToolResultPart.result``.
# ``"special"`` means a :class:`SpecialToolResult`; ``"error"`` flags
Expand Down Expand Up @@ -174,7 +178,7 @@ class ToolResultPart(pydantic.BaseModel):
# again, though.
model_input: Any = pydantic.Field(
default_factory=lambda: _MODEL_INPUT_UNSET,
exclude_if=lambda v: v is _MODEL_INPUT_UNSET,
exclude_if=lambda v: isinstance(v, _ModelInputUnset),
repr=False,
)

Expand Down
32 changes: 32 additions & 0 deletions tests/types/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,35 @@ def test_tool_result_file_part_base64_valid_after_round_trip() -> None:
assert "-" not in b64
decoded = base64.b64decode(b64)
assert decoded == raw


def test_tool_result_without_model_input_serializes_after_deep_copy() -> None:
"""A deep-copied ToolResultPart with no model_input still serializes.

``model_input`` defaults to the ``_MODEL_INPUT_UNSET`` singleton and is
dropped from output via ``exclude_if=lambda v: v is _MODEL_INPUT_UNSET``,
an identity check. ``model_copy(deep=True)`` rebuilds the sentinel into a
*new* instance, so the identity check fails, the field is no longer
excluded, and pydantic tries to serialize the bare sentinel. Client apps
that deep-copy messages hit this on serialize.
"""
msg = messages.Message(
role="tool",
parts=[
messages.ToolResultPart(
tool_call_id="tc", tool_name="t", result={"ok": 1}
)
],
)
part = msg.parts[0]
assert isinstance(part, messages.ToolResultPart)
assert not part.has_model_input

cloned = msg.model_copy(deep=True)

j = cloned.model_dump_json()
restored = messages.Message.model_validate_json(j)
rpart = restored.parts[0]
assert isinstance(rpart, messages.ToolResultPart)
assert rpart.result == {"ok": 1}
assert not rpart.has_model_input
Loading