Skip to content

Conversation

alexmojaki
Copy link
Contributor

A user reported an error being thrown by .cost(): https://pydanticlogfire.slack.com/archives/C083V7PMHHA/p1757110611141589

I can't repro this, but in general instrumentation shouldn't cause errors.

"opentelemetry-api>=1.28.0",
"typing-inspection>=0.4.0",
"genai-prices>=0.0.22",
"genai-prices>=0.0.23",
Copy link
Contributor Author

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.

@alexmojaki alexmojaki requested a review from Copilot September 8, 2025 13:00
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves error handling in the cost calculation within InstrumentedModel to prevent instrumentation failures. The change ensures that any unexpected errors during cost calculation result in a warning rather than crashing the instrumentation.

  • Added comprehensive exception handling for cost calculation errors
  • Updated the genai-prices dependency to version 0.0.23
  • Added test coverage for cost calculation error scenarios

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
pydantic_ai_slim/pydantic_ai/models/instrumented.py Added exception handling for cost calculation and CostCalculationFailedWarning class
tests/models/test_instrumented.py Added test case to verify warning behavior when cost calculation fails
pydantic_ai_slim/pyproject.toml Updated genai-prices dependency version to 0.0.23

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link

github-actions bot commented Sep 8, 2025

Docs Preview

commit: a05f689
Preview URL: https://5f889bdf-pydantic-ai-previews.pydantic.workers.dev


import pytest
from inline_snapshot import snapshot
from inline_snapshot.extra import warns
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cute.

@alexmojaki alexmojaki merged commit 5f9d8de into main Sep 8, 2025
60 of 62 checks passed
@alexmojaki alexmojaki deleted the alex/cost-error branch September 8, 2025 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants