Skip to content

Add model_request_parameters attribute (containing tool definitions) to chat spans #1177

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

Merged
merged 1 commit into from
Mar 22, 2025
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
17 changes: 14 additions & 3 deletions pydantic_ai_slim/pydantic_ai/models/instrumented.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def request(
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> tuple[ModelResponse, Usage]:
with self._instrument(messages, model_settings) as finish:
with self._instrument(messages, model_settings, model_request_parameters) as finish:
response, usage = await super().request(messages, model_settings, model_request_parameters)
finish(response, usage)
return response, usage
Expand All @@ -124,7 +124,7 @@ async def request_stream(
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> AsyncIterator[StreamedResponse]:
with self._instrument(messages, model_settings) as finish:
with self._instrument(messages, model_settings, model_request_parameters) as finish:
response_stream: StreamedResponse | None = None
try:
async with super().request_stream(
Expand All @@ -140,6 +140,7 @@ def _instrument(
self,
messages: list[ModelMessage],
model_settings: ModelSettings | None,
model_request_parameters: ModelRequestParameters,
) -> Iterator[Callable[[ModelResponse, Usage], None]]:
operation = 'chat'
span_name = f'{operation} {self.model_name}'
Expand All @@ -149,6 +150,13 @@ def _instrument(
attributes: dict[str, AttributeValue] = {
'gen_ai.operation.name': operation,
**self.model_attributes(self.wrapped),
'model_request_parameters': json.dumps(InstrumentedModel.serialize_any(model_request_parameters)),
'logfire.json_schema': json.dumps(
{
'type': 'object',
'properties': {'model_request_parameters': {'type': 'object'}},
}
),
}

if model_settings:
Expand Down Expand Up @@ -201,7 +209,10 @@ def _emit_events(self, span: Span, events: list[Event]) -> None:
'logfire.json_schema': json.dumps(
{
'type': 'object',
'properties': {attr_name: {'type': 'array'}},
'properties': {
attr_name: {'type': 'array'},
'model_request_parameters': {'type': 'object'},
},
}
),
}
Expand Down
6 changes: 4 additions & 2 deletions tests/models/test_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def test_first_failed_instrumented(capfire: CaptureLogfire) -> None:
'end_time': 5000000000,
'attributes': {
'gen_ai.operation.name': 'chat',
'model_request_parameters': '{"function_tools": [], "allow_text_result": true, "result_tools": []}',
'logfire.span_type': 'span',
'logfire.msg': 'chat fallback:function:failure_response:,function:success_response:',
'gen_ai.usage.input_tokens': 51,
Expand All @@ -160,7 +161,7 @@ def test_first_failed_instrumented(capfire: CaptureLogfire) -> None:
},
]
),
'logfire.json_schema': '{"type": "object", "properties": {"events": {"type": "array"}}}',
'logfire.json_schema': '{"type": "object", "properties": {"events": {"type": "array"}, "model_request_parameters": {"type": "object"}}}',
},
},
{
Expand Down Expand Up @@ -233,6 +234,7 @@ async def test_first_failed_instrumented_stream(capfire: CaptureLogfire) -> None
'end_time': 5000000000,
'attributes': {
'gen_ai.operation.name': 'chat',
'model_request_parameters': '{"function_tools": [], "allow_text_result": true, "result_tools": []}',
'logfire.span_type': 'span',
'logfire.msg': 'chat fallback:function::failure_response_stream,function::success_response_stream',
'gen_ai.system': 'function',
Expand All @@ -241,7 +243,7 @@ async def test_first_failed_instrumented_stream(capfire: CaptureLogfire) -> None
'gen_ai.usage.output_tokens': 2,
'gen_ai.response.model': 'function::success_response_stream',
'events': '[{"content": "input", "role": "user", "gen_ai.system": "function", "gen_ai.message.index": 0, "event.name": "gen_ai.user.message"}, {"index": 0, "message": {"role": "assistant", "content": "hello world"}, "gen_ai.system": "function", "event.name": "gen_ai.choice"}]',
'logfire.json_schema': '{"type": "object", "properties": {"events": {"type": "array"}}}',
'logfire.json_schema': '{"type": "object", "properties": {"events": {"type": "array"}, "model_request_parameters": {"type": "object"}}}',
},
},
{
Expand Down
9 changes: 8 additions & 1 deletion tests/models/test_instrumented.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ async def test_instrumented_model(capfire: CaptureLogfire):
'gen_ai.request.model': 'my_model',
'server.address': 'example.com',
'server.port': 8000,
'model_request_parameters': '{"function_tools": [], "allow_text_result": true, "result_tools": []}',
'logfire.json_schema': '{"type": "object", "properties": {"model_request_parameters": {"type": "object"}}}',
'gen_ai.request.temperature': 1,
'logfire.msg': 'chat my_model',
'logfire.span_type': 'span',
Expand Down Expand Up @@ -374,6 +376,8 @@ async def test_instrumented_model_stream(capfire: CaptureLogfire):
'gen_ai.request.model': 'my_model',
'server.address': 'example.com',
'server.port': 8000,
'model_request_parameters': '{"function_tools": [], "allow_text_result": true, "result_tools": []}',
'logfire.json_schema': '{"type": "object", "properties": {"model_request_parameters": {"type": "object"}}}',
'gen_ai.request.temperature': 1,
'logfire.msg': 'chat my_model',
'logfire.span_type': 'span',
Expand Down Expand Up @@ -457,6 +461,8 @@ async def test_instrumented_model_stream_break(capfire: CaptureLogfire):
'gen_ai.request.model': 'my_model',
'server.address': 'example.com',
'server.port': 8000,
'model_request_parameters': '{"function_tools": [], "allow_text_result": true, "result_tools": []}',
'logfire.json_schema': '{"type": "object", "properties": {"model_request_parameters": {"type": "object"}}}',
'gen_ai.request.temperature': 1,
'logfire.msg': 'chat my_model',
'logfire.span_type': 'span',
Expand Down Expand Up @@ -559,6 +565,7 @@ async def test_instrumented_model_attributes_mode(capfire: CaptureLogfire):
'gen_ai.request.model': 'my_model',
'server.address': 'example.com',
'server.port': 8000,
'model_request_parameters': '{"function_tools": [], "allow_text_result": true, "result_tools": []}',
'gen_ai.request.temperature': 1,
'logfire.msg': 'chat my_model',
'logfire.span_type': 'span',
Expand Down Expand Up @@ -652,7 +659,7 @@ async def test_instrumented_model_attributes_mode(capfire: CaptureLogfire):
]
)
),
'logfire.json_schema': '{"type": "object", "properties": {"events": {"type": "array"}}}',
'logfire.json_schema': '{"type": "object", "properties": {"events": {"type": "array"}, "model_request_parameters": {"type": "object"}}}',
},
},
]
Expand Down
25 changes: 23 additions & 2 deletions tests/test_logfire.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ async def my_ret(x: int) -> str:
)
chat_span_attributes = summary.attributes[2]
if instrument is True or instrument.event_mode == 'attributes':
attribute_mode_attributes = {k: chat_span_attributes.pop(k) for k in ['events', 'logfire.json_schema']}
attribute_mode_attributes = {k: chat_span_attributes.pop(k) for k in ['events']}
assert attribute_mode_attributes == snapshot(
{
'events': IsJson(
Expand Down Expand Up @@ -198,7 +198,6 @@ async def my_ret(x: int) -> str:
]
)
),
'logfire.json_schema': '{"type": "object", "properties": {"events": {"type": "array"}}}',
}
)

Expand All @@ -207,6 +206,28 @@ async def my_ret(x: int) -> str:
'gen_ai.operation.name': 'chat',
'gen_ai.system': 'test',
'gen_ai.request.model': 'test',
'model_request_parameters': IsJson(
snapshot(
{
'function_tools': [
{
'name': 'my_ret',
'description': '',
'parameters_json_schema': {
'additionalProperties': False,
'properties': {'x': {'type': 'integer'}},
'required': ['x'],
'type': 'object',
},
'outer_typed_dict_key': None,
}
],
'allow_text_result': True,
'result_tools': [],
}
)
),
'logfire.json_schema': IsJson(),
'logfire.span_type': 'span',
'logfire.msg': 'chat test',
'gen_ai.response.model': 'test',
Expand Down