From d5fddc6514cbfa256d79afa34830f0fa4c82abbd Mon Sep 17 00:00:00 2001 From: adtyavrdhn Date: Fri, 14 Nov 2025 13:44:28 +0530 Subject: [PATCH 1/3] Adding metadata --- pydantic_ai_slim/pydantic_ai/messages.py | 6 ++++++ tests/test_messages.py | 7 +++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/messages.py b/pydantic_ai_slim/pydantic_ai/messages.py index f2e3d5eef8..e35124e37a 100644 --- a/pydantic_ai_slim/pydantic_ai/messages.py +++ b/pydantic_ai_slim/pydantic_ai/messages.py @@ -951,6 +951,9 @@ class ModelRequest: run_id: str | None = None """The unique identifier of the agent run in which this message originated.""" + metadata: dict[str, Any] | None = None + """Additional data tagged on the message but is not sent to the model""" + @classmethod def user_text_prompt(cls, user_prompt: str, *, instructions: str | None = None) -> ModelRequest: """Create a `ModelRequest` with a single user prompt as text.""" @@ -1195,6 +1198,9 @@ class ModelResponse: run_id: str | None = None """The unique identifier of the agent run in which this message originated.""" + metadata: dict[str, Any] | None = None + """Additional data tagged on the message but is not sent to the model""" + @property def text(self) -> str | None: """Get the text in the response.""" diff --git a/tests/test_messages.py b/tests/test_messages.py index 9b6ad3fbb8..d971ac46b3 100644 --- a/tests/test_messages.py +++ b/tests/test_messages.py @@ -463,6 +463,7 @@ def test_file_part_serialization_roundtrip(): 'provider_response_id': None, 'finish_reason': None, 'run_id': None, + 'metadata': None, } ] ) @@ -475,11 +476,9 @@ def test_model_messages_type_adapter_preserves_run_id(): ModelRequest( parts=[UserPromptPart(content='Hi there', timestamp=datetime.now(tz=timezone.utc))], run_id='run-123', + metadata={'key': 'value'}, ), - ModelResponse( - parts=[TextPart(content='Hello!')], - run_id='run-123', - ), + ModelResponse(parts=[TextPart(content='Hello!')], run_id='run-123', metadata={'key': 'value'}), ] serialized = ModelMessagesTypeAdapter.dump_python(messages, mode='python') From 83a1c1749f481a9cb18ac88c7118b383c7b44135 Mon Sep 17 00:00:00 2001 From: adtyavrdhn Date: Fri, 14 Nov 2025 14:55:05 +0530 Subject: [PATCH 2/3] Adding metadata in test_agent --- tests/test_agent.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_agent.py b/tests/test_agent.py index d8323f3c98..29b54643f0 100644 --- a/tests/test_agent.py +++ b/tests/test_agent.py @@ -3730,6 +3730,7 @@ def test_binary_content_serializable(): 'instructions': None, 'kind': 'request', 'run_id': IsStr(), + 'metadata': None, }, { 'parts': [{'content': 'success (no tool calls)', 'id': None, 'part_kind': 'text'}], @@ -3751,6 +3752,7 @@ def test_binary_content_serializable(): 'kind': 'response', 'finish_reason': None, 'run_id': IsStr(), + 'metadata': None, }, ] ) @@ -3788,6 +3790,7 @@ def test_image_url_serializable_missing_media_type(): 'instructions': None, 'kind': 'request', 'run_id': IsStr(), + 'metadata': None, }, { 'parts': [{'content': 'success (no tool calls)', 'id': None, 'part_kind': 'text'}], @@ -3809,6 +3812,7 @@ def test_image_url_serializable_missing_media_type(): 'kind': 'response', 'finish_reason': None, 'run_id': IsStr(), + 'metadata': None, }, ] ) @@ -3853,6 +3857,7 @@ def test_image_url_serializable(): 'instructions': None, 'kind': 'request', 'run_id': IsStr(), + 'metadata': None, }, { 'parts': [{'content': 'success (no tool calls)', 'id': None, 'part_kind': 'text'}], @@ -3874,6 +3879,7 @@ def test_image_url_serializable(): 'kind': 'response', 'finish_reason': None, 'run_id': IsStr(), + 'metadata': None, }, ] ) @@ -4353,6 +4359,7 @@ def foo_tool(foo: Foo) -> int: 'instructions': None, 'kind': 'request', 'run_id': IsStr(), + 'metadata': None, } ) From a7cef3ddfbce029669d58b65f8e2f40c453cf32d Mon Sep 17 00:00:00 2001 From: adtyavrdhn Date: Tue, 18 Nov 2025 08:32:17 +0530 Subject: [PATCH 3/3] docstring --- pydantic_ai_slim/pydantic_ai/messages.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/messages.py b/pydantic_ai_slim/pydantic_ai/messages.py index b18ddd3ed7..c880c75a74 100644 --- a/pydantic_ai_slim/pydantic_ai/messages.py +++ b/pydantic_ai_slim/pydantic_ai/messages.py @@ -971,7 +971,7 @@ class ModelRequest: """The unique identifier of the agent run in which this message originated.""" metadata: dict[str, Any] | None = None - """Additional data tagged on the message but is not sent to the model""" + """Additional data that can be accessed programmatically by the application but is not sent to the LLM.""" @classmethod def user_text_prompt(cls, user_prompt: str, *, instructions: str | None = None) -> ModelRequest: @@ -1218,7 +1218,7 @@ class ModelResponse: """The unique identifier of the agent run in which this message originated.""" metadata: dict[str, Any] | None = None - """Additional data tagged on the message but is not sent to the model""" + """Additional data that can be accessed programmatically by the application but is not sent to the LLM.""" @property def text(self) -> str | None: