Skip to content

Commit 9e67fe3

Browse files
authored
Add support for gen_ai.response.id (#2831)
1 parent 3c2f1cf commit 9e67fe3

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

pydantic_ai_slim/pydantic_ai/models/instrumented.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -424,13 +424,15 @@ def _record_metrics():
424424
cost_attributes = {'operation.cost': float(response.cost().total_price)}
425425
except LookupError:
426426
cost_attributes = {}
427-
span.set_attributes(
428-
{
429-
**response.usage.opentelemetry_attributes(),
430-
'gen_ai.response.model': response_model,
431-
**cost_attributes,
432-
}
433-
)
427+
428+
attributes_to_set = {
429+
**response.usage.opentelemetry_attributes(),
430+
'gen_ai.response.model': response_model,
431+
**cost_attributes,
432+
}
433+
if response.provider_response_id is not None:
434+
attributes_to_set['gen_ai.response.id'] = response.provider_response_id
435+
span.set_attributes(attributes_to_set)
434436
span.update_name(f'{operation} {request_model}')
435437

436438
yield finish

tests/models/test_instrumented.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ async def request(
8686
usage=RequestUsage(input_tokens=100, output_tokens=200),
8787
model_name='gpt-4o-2024-11-20',
8888
provider_details=dict(finish_reason='stop', foo='bar'),
89+
provider_response_id='response_id',
8990
)
9091

9192
@asynccontextmanager
@@ -183,6 +184,7 @@ async def test_instrumented_model(capfire: CaptureLogfire):
183184
'logfire.msg': 'chat gpt-4o',
184185
'logfire.span_type': 'span',
185186
'gen_ai.response.model': 'gpt-4o-2024-11-20',
187+
'gen_ai.response.id': 'response_id',
186188
'gen_ai.usage.input_tokens': 100,
187189
'gen_ai.usage.output_tokens': 200,
188190
'operation.cost': 0.00225,
@@ -719,6 +721,7 @@ async def test_instrumented_model_attributes_mode(capfire: CaptureLogfire, instr
719721
'properties': {'events': {'type': 'array'}, 'model_request_parameters': {'type': 'object'}},
720722
},
721723
'operation.cost': 0.00225,
724+
'gen_ai.response.id': 'response_id',
722725
},
723726
},
724727
]
@@ -820,6 +823,7 @@ async def test_instrumented_model_attributes_mode(capfire: CaptureLogfire, instr
820823
},
821824
},
822825
'operation.cost': 0.00225,
826+
'gen_ai.response.id': 'response_id',
823827
},
824828
},
825829
]

0 commit comments

Comments
 (0)