Skip to content
Open
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
3 changes: 2 additions & 1 deletion pydantic_ai_slim/pydantic_ai/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2025,7 +2025,8 @@ def _model_request_part_discriminator(v: Any) -> str | None:
| Annotated[UserPromptPart, pydantic.Tag('user-prompt')]
| Annotated[ToolSearchReturnPart, pydantic.Tag('tool-search-return')]
| Annotated[ToolReturnPart, pydantic.Tag('tool-return')]
| Annotated[RetryPromptPart, pydantic.Tag('retry-prompt')],
| Annotated[RetryPromptPart, pydantic.Tag('retry-prompt')]
| Annotated[InstructionPart, pydantic.Tag('instruction')],
pydantic.Discriminator(_model_request_part_discriminator),
]
"""A message part sent by Pydantic AI to a model."""
Expand Down
38 changes: 38 additions & 0 deletions tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1508,6 +1508,44 @@ def test_repr(self):
dynamic_part = InstructionPart(content='world', dynamic=True)
assert repr(dynamic_part) == "InstructionPart(content='world', dynamic=True)"

def test_instruction_part_in_parts_roundtrip(self):
"""InstructionPart inside ModelRequest.parts must survive a dump_json/validate_json round-trip.

Regression test for https://github.com/pydantic/pydantic-ai/issues/5696 —
InstructionPart was missing from the ModelRequestPart discriminated union, so
validate_json raised ValidationError for any serialized message history that
contained an InstructionPart in its parts list.
"""
request = ModelRequest(
parts=[
UserPromptPart(content='Hello'),
InstructionPart(content='Be concise.', dynamic=False),
InstructionPart(content='Always respond in English.', dynamic=True),
]
)

serialized = ModelMessagesTypeAdapter.dump_json([request])
deserialized = ModelMessagesTypeAdapter.validate_json(serialized)

assert len(deserialized) == 1
msg = deserialized[0]
assert isinstance(msg, ModelRequest)
assert len(msg.parts) == 3

user_part = msg.parts[0]
assert isinstance(user_part, UserPromptPart)
assert user_part.content == 'Hello'

static_instr = msg.parts[1]
assert isinstance(static_instr, InstructionPart)
assert static_instr.content == 'Be concise.'
assert static_instr.dynamic is False

dynamic_instr = msg.parts[2]
assert isinstance(dynamic_instr, InstructionPart)
assert dynamic_instr.content == 'Always respond in English.'
assert dynamic_instr.dynamic is True


def test_retry_prompt_strips_input_from_top_level_errors():
"""Top-level validation errors should not include `input` in model_response() since it duplicates the entire generated output."""
Expand Down
Loading