|
7 | 7 |
|
8 | 8 | import pytest
|
9 | 9 | from inline_snapshot import snapshot
|
| 10 | +from inline_snapshot.extra import warns |
10 | 11 | from logfire_api import DEFAULT_LOGFIRE_INSTANCE
|
11 | 12 | from opentelemetry._events import NoOpEventLoggerProvider
|
12 | 13 | from opentelemetry.trace import NoOpTracerProvider
|
@@ -1278,3 +1279,73 @@ def test_deprecated_event_mode_warning():
|
1278 | 1279 | assert settings.event_mode == 'logs'
|
1279 | 1280 | assert settings.version == 1
|
1280 | 1281 | assert InstrumentationSettings().version == 2
|
| 1282 | + |
| 1283 | + |
| 1284 | +async def test_response_cost_error(capfire: CaptureLogfire, monkeypatch: pytest.MonkeyPatch): |
| 1285 | + model = InstrumentedModel(MyModel()) |
| 1286 | + |
| 1287 | + messages: list[ModelMessage] = [ModelRequest(parts=[UserPromptPart('user_prompt')])] |
| 1288 | + monkeypatch.setattr(ModelResponse, 'cost', None) |
| 1289 | + |
| 1290 | + with warns( |
| 1291 | + snapshot( |
| 1292 | + [ |
| 1293 | + "CostCalculationFailedWarning: Failed to get cost from response: TypeError: 'NoneType' object is not callable" |
| 1294 | + ] |
| 1295 | + ) |
| 1296 | + ): |
| 1297 | + await model.request(messages, model_settings=ModelSettings(), model_request_parameters=ModelRequestParameters()) |
| 1298 | + |
| 1299 | + assert capfire.exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot( |
| 1300 | + [ |
| 1301 | + { |
| 1302 | + 'name': 'chat gpt-4o', |
| 1303 | + 'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, |
| 1304 | + 'parent': None, |
| 1305 | + 'start_time': 1000000000, |
| 1306 | + 'end_time': 2000000000, |
| 1307 | + 'attributes': { |
| 1308 | + 'gen_ai.operation.name': 'chat', |
| 1309 | + 'gen_ai.system': 'openai', |
| 1310 | + 'gen_ai.request.model': 'gpt-4o', |
| 1311 | + 'server.address': 'example.com', |
| 1312 | + 'server.port': 8000, |
| 1313 | + 'model_request_parameters': { |
| 1314 | + 'function_tools': [], |
| 1315 | + 'builtin_tools': [], |
| 1316 | + 'output_mode': 'text', |
| 1317 | + 'output_object': None, |
| 1318 | + 'output_tools': [], |
| 1319 | + 'allow_text_output': True, |
| 1320 | + }, |
| 1321 | + 'logfire.span_type': 'span', |
| 1322 | + 'logfire.msg': 'chat gpt-4o', |
| 1323 | + 'gen_ai.input.messages': [{'role': 'user', 'parts': [{'type': 'text', 'content': 'user_prompt'}]}], |
| 1324 | + 'gen_ai.output.messages': [ |
| 1325 | + { |
| 1326 | + 'role': 'assistant', |
| 1327 | + 'parts': [ |
| 1328 | + {'type': 'text', 'content': 'text1'}, |
| 1329 | + {'type': 'tool_call', 'id': 'tool_call_1', 'name': 'tool1', 'arguments': 'args1'}, |
| 1330 | + {'type': 'tool_call', 'id': 'tool_call_2', 'name': 'tool2', 'arguments': {'args2': 3}}, |
| 1331 | + {'type': 'text', 'content': 'text2'}, |
| 1332 | + ], |
| 1333 | + 'finish_reason': 'stop', |
| 1334 | + } |
| 1335 | + ], |
| 1336 | + 'logfire.json_schema': { |
| 1337 | + 'type': 'object', |
| 1338 | + 'properties': { |
| 1339 | + 'gen_ai.input.messages': {'type': 'array'}, |
| 1340 | + 'gen_ai.output.messages': {'type': 'array'}, |
| 1341 | + 'model_request_parameters': {'type': 'object'}, |
| 1342 | + }, |
| 1343 | + }, |
| 1344 | + 'gen_ai.usage.input_tokens': 100, |
| 1345 | + 'gen_ai.usage.output_tokens': 200, |
| 1346 | + 'gen_ai.response.model': 'gpt-4o-2024-11-20', |
| 1347 | + 'gen_ai.response.id': 'response_id', |
| 1348 | + }, |
| 1349 | + } |
| 1350 | + ] |
| 1351 | + ) |
0 commit comments