-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Handle errors in cost calculation in InstrumentedModel #2834
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
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6e4c107
Handle errors in cost calculation in InstrumentedModel
alexmojaki bf17a8d
comments
alexmojaki e9003ec
Update test_anthropic for updated genai-prices
alexmojaki d7594bf
Merge branch 'main' of github.com:pydantic/pydantic-ai into alex/cost…
alexmojaki a05f689
Merge branch 'main' of github.com:pydantic/pydantic-ai into alex/cost…
alexmojaki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
import pytest | ||
from inline_snapshot import snapshot | ||
from inline_snapshot.extra import warns | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cute. |
||
from logfire_api import DEFAULT_LOGFIRE_INSTANCE | ||
from opentelemetry._events import NoOpEventLoggerProvider | ||
from opentelemetry.trace import NoOpTracerProvider | ||
|
@@ -1274,3 +1275,72 @@ def test_deprecated_event_mode_warning(): | |
assert settings.event_mode == 'logs' | ||
assert settings.version == 1 | ||
assert InstrumentationSettings().version == 2 | ||
|
||
|
||
async def test_response_cost_error(capfire: CaptureLogfire, monkeypatch: pytest.MonkeyPatch): | ||
model = InstrumentedModel(MyModel()) | ||
|
||
messages: list[ModelMessage] = [ModelRequest(parts=[UserPromptPart('user_prompt')])] | ||
monkeypatch.setattr(ModelResponse, 'cost', None) | ||
|
||
with warns( | ||
snapshot( | ||
[ | ||
"CostCalculationFailedWarning: Failed to get cost from response: TypeError: 'NoneType' object is not callable" | ||
] | ||
) | ||
): | ||
await model.request(messages, model_settings=ModelSettings(), model_request_parameters=ModelRequestParameters()) | ||
|
||
assert capfire.exporter.exported_spans_as_dict(parse_json_attributes=True) == snapshot( | ||
[ | ||
{ | ||
'name': 'chat gpt-4o', | ||
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False}, | ||
'parent': None, | ||
'start_time': 1000000000, | ||
'end_time': 2000000000, | ||
'attributes': { | ||
'gen_ai.operation.name': 'chat', | ||
'gen_ai.system': 'openai', | ||
'gen_ai.request.model': 'gpt-4o', | ||
'server.address': 'example.com', | ||
'server.port': 8000, | ||
'model_request_parameters': { | ||
'function_tools': [], | ||
'builtin_tools': [], | ||
'output_mode': 'text', | ||
'output_object': None, | ||
'output_tools': [], | ||
'allow_text_output': True, | ||
}, | ||
'logfire.span_type': 'span', | ||
'logfire.msg': 'chat gpt-4o', | ||
'gen_ai.input.messages': [{'role': 'user', 'parts': [{'type': 'text', 'content': 'user_prompt'}]}], | ||
'gen_ai.output.messages': [ | ||
{ | ||
'role': 'assistant', | ||
'parts': [ | ||
{'type': 'text', 'content': 'text1'}, | ||
{'type': 'tool_call', 'id': 'tool_call_1', 'name': 'tool1', 'arguments': 'args1'}, | ||
{'type': 'tool_call', 'id': 'tool_call_2', 'name': 'tool2', 'arguments': {'args2': 3}}, | ||
{'type': 'text', 'content': 'text2'}, | ||
], | ||
'finish_reason': 'stop', | ||
} | ||
], | ||
'logfire.json_schema': { | ||
'type': 'object', | ||
'properties': { | ||
'gen_ai.input.messages': {'type': 'array'}, | ||
'gen_ai.output.messages': {'type': 'array'}, | ||
'model_request_parameters': {'type': 'object'}, | ||
}, | ||
}, | ||
'gen_ai.usage.input_tokens': 100, | ||
'gen_ai.usage.output_tokens': 200, | ||
'gen_ai.response.model': 'gpt-4o-2024-11-20', | ||
}, | ||
} | ||
] | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a significant fix in price calculation logic released here, so I think this needs to be the minimum.