Skip to content

Added request_id to the model response. #1547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions docs/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ async def main():
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
)
),
End(data=FinalResult(output='Paris', tool_name=None, tool_call_id=None)),
Expand Down Expand Up @@ -212,6 +213,7 @@ async def main():
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
)
),
End(data=FinalResult(output='Paris', tool_name=None, tool_call_id=None)),
Expand Down Expand Up @@ -808,6 +810,7 @@ with capture_run_messages() as messages: # (2)!
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
ModelRequest(
parts=[
Expand All @@ -834,6 +837,7 @@ with capture_run_messages() as messages: # (2)!
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
]
"""
Expand Down
6 changes: 6 additions & 0 deletions docs/message-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ print(result.all_messages())
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
]
"""
Expand Down Expand Up @@ -145,6 +146,7 @@ async def main():
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
]
"""
Expand Down Expand Up @@ -204,6 +206,7 @@ print(result2.all_messages())
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
ModelRequest(
parts=[
Expand All @@ -226,6 +229,7 @@ print(result2.all_messages())
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
]
"""
Expand Down Expand Up @@ -332,6 +336,7 @@ print(result2.all_messages())
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
ModelRequest(
parts=[
Expand All @@ -354,6 +359,7 @@ print(result2.all_messages())
model_name='gemini-1.5-pro',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
]
"""
Expand Down
3 changes: 3 additions & 0 deletions docs/tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ print(dice_result.all_messages())
model_name='gemini-1.5-flash',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
ModelRequest(
parts=[
Expand All @@ -125,6 +126,7 @@ print(dice_result.all_messages())
model_name='gemini-1.5-flash',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
ModelRequest(
parts=[
Expand All @@ -149,6 +151,7 @@ print(dice_result.all_messages())
model_name='gemini-1.5-flash',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
),
]
"""
Expand Down
3 changes: 3 additions & 0 deletions pydantic_ai_slim/pydantic_ai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,7 @@ async def main():
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
)
),
End(data=FinalResult(output='Paris', tool_name=None, tool_call_id=None)),
Expand Down Expand Up @@ -1717,6 +1718,7 @@ async def main():
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
)
),
End(data=FinalResult(output='Paris', tool_name=None, tool_call_id=None)),
Expand Down Expand Up @@ -1855,6 +1857,7 @@ async def main():
model_name='gpt-4o',
timestamp=datetime.datetime(...),
kind='response',
vendor_id=None,
)
),
End(data=FinalResult(output='Paris', tool_name=None, tool_call_id=None)),
Expand Down
3 changes: 3 additions & 0 deletions pydantic_ai_slim/pydantic_ai/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ class ModelResponse:
kind: Literal['response'] = 'response'
"""Message type identifier, this is available on all parts as a discriminator."""

vendor_id: str | None = None
"""Vendor ID as specified by the model provider. This can be used to track the specific request to the model."""

def otel_events(self) -> list[Event]:
"""Return OpenTelemetry events for the response."""
result: list[Event] = []
Expand Down
3 changes: 2 additions & 1 deletion pydantic_ai_slim/pydantic_ai/models/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ async def _process_response(self, response: ConverseResponseTypeDef) -> tuple[Mo
response_tokens=response['usage']['outputTokens'],
total_tokens=response['usage']['totalTokens'],
)
return ModelResponse(items, model_name=self.model_name), u
vendor_id = response.get('ResponseMetadata', {}).get('RequestId', None)
return ModelResponse(items, model_name=self.model_name, vendor_id=vendor_id), u

@overload
async def _messages_create(
Expand Down
1 change: 1 addition & 0 deletions tests/test_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,7 @@ def test_binary_content_all_messages_json():
{
'parts': [{'content': 'success (no tool calls)', 'part_kind': 'text'}],
'model_name': 'test',
'vendor_id': None,
'timestamp': IsStr(),
'kind': 'response',
},
Expand Down