Skip to content

Commit d218143

Browse files
adtyavrdhnDouweM
andauthored
Adds {ModelRequest,ModelResponse}.metadata fields (#3422)
Co-authored-by: Douwe Maan <[email protected]>
1 parent 3450322 commit d218143

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

pydantic_ai_slim/pydantic_ai/messages.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,6 +970,9 @@ class ModelRequest:
970970
run_id: str | None = None
971971
"""The unique identifier of the agent run in which this message originated."""
972972

973+
metadata: dict[str, Any] | None = None
974+
"""Additional data that can be accessed programmatically by the application but is not sent to the LLM."""
975+
973976
@classmethod
974977
def user_text_prompt(cls, user_prompt: str, *, instructions: str | None = None) -> ModelRequest:
975978
"""Create a `ModelRequest` with a single user prompt as text."""
@@ -1214,6 +1217,9 @@ class ModelResponse:
12141217
run_id: str | None = None
12151218
"""The unique identifier of the agent run in which this message originated."""
12161219

1220+
metadata: dict[str, Any] | None = None
1221+
"""Additional data that can be accessed programmatically by the application but is not sent to the LLM."""
1222+
12171223
@property
12181224
def text(self) -> str | None:
12191225
"""Get the text in the response."""

tests/test_agent.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3730,6 +3730,7 @@ def test_binary_content_serializable():
37303730
'instructions': None,
37313731
'kind': 'request',
37323732
'run_id': IsStr(),
3733+
'metadata': None,
37333734
},
37343735
{
37353736
'parts': [{'content': 'success (no tool calls)', 'id': None, 'part_kind': 'text'}],
@@ -3751,6 +3752,7 @@ def test_binary_content_serializable():
37513752
'kind': 'response',
37523753
'finish_reason': None,
37533754
'run_id': IsStr(),
3755+
'metadata': None,
37543756
},
37553757
]
37563758
)
@@ -3788,6 +3790,7 @@ def test_image_url_serializable_missing_media_type():
37883790
'instructions': None,
37893791
'kind': 'request',
37903792
'run_id': IsStr(),
3793+
'metadata': None,
37913794
},
37923795
{
37933796
'parts': [{'content': 'success (no tool calls)', 'id': None, 'part_kind': 'text'}],
@@ -3809,6 +3812,7 @@ def test_image_url_serializable_missing_media_type():
38093812
'kind': 'response',
38103813
'finish_reason': None,
38113814
'run_id': IsStr(),
3815+
'metadata': None,
38123816
},
38133817
]
38143818
)
@@ -3853,6 +3857,7 @@ def test_image_url_serializable():
38533857
'instructions': None,
38543858
'kind': 'request',
38553859
'run_id': IsStr(),
3860+
'metadata': None,
38563861
},
38573862
{
38583863
'parts': [{'content': 'success (no tool calls)', 'id': None, 'part_kind': 'text'}],
@@ -3874,6 +3879,7 @@ def test_image_url_serializable():
38743879
'kind': 'response',
38753880
'finish_reason': None,
38763881
'run_id': IsStr(),
3882+
'metadata': None,
38773883
},
38783884
]
38793885
)
@@ -4353,6 +4359,7 @@ def foo_tool(foo: Foo) -> int:
43534359
'instructions': None,
43544360
'kind': 'request',
43554361
'run_id': IsStr(),
4362+
'metadata': None,
43564363
}
43574364
)
43584365

tests/test_messages.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ def test_file_part_serialization_roundtrip():
463463
'provider_response_id': None,
464464
'finish_reason': None,
465465
'run_id': None,
466+
'metadata': None,
466467
}
467468
]
468469
)
@@ -475,11 +476,9 @@ def test_model_messages_type_adapter_preserves_run_id():
475476
ModelRequest(
476477
parts=[UserPromptPart(content='Hi there', timestamp=datetime.now(tz=timezone.utc))],
477478
run_id='run-123',
479+
metadata={'key': 'value'},
478480
),
479-
ModelResponse(
480-
parts=[TextPart(content='Hello!')],
481-
run_id='run-123',
482-
),
481+
ModelResponse(parts=[TextPart(content='Hello!')], run_id='run-123', metadata={'key': 'value'}),
483482
]
484483

485484
serialized = ModelMessagesTypeAdapter.dump_python(messages, mode='python')

0 commit comments

Comments
 (0)