Skip to content
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

Validate chatcompletion to avoid unexpected bugs #551

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sambhav
Copy link

@sambhav sambhav commented Dec 26, 2024

Fixes #527

This PR addresses the incorrect handling of error responses from the API when a rate limit is exceeded. The changes include:

  • Adding a check for the presence of a timestamp in the API response and raising an appropriate error if it is missing.
  • Updating the _process_response method in pydantic_ai/models/openai.py to handle the absence of a timestamp.
  • Adding new tests in tests/models/test_openai.py to ensure proper error handling when the timestamp is missing from the response.

These changes ensure that the appropriate error message is surfaced when a rate limit is exceeded, instead of throwing a date-time parsing exception.

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Hi @sambhav,

Thanks for your contribution.

A few things:

  1. We can probably implement a fix both in _process_response and _process_streamed_response.
  2. To be more consistent with other models (like gemini), let's just do something like the following instead of raising an error
if response.created:
    timestamp = datetime.fromtimestamp(response.created, tz=timezone.utc)
else:
    timestamp = _utils.now_utc()

@sambhav
Copy link
Author

sambhav commented Dec 27, 2024

@sydney-runkle that might be a sane default for the timestamp, but as mentioned in the issue, the bug arises from the fact that it's not a chat completion but instead an error response. As a result, the other fields will be undefined.

@sydney-runkle
Copy link
Member

instead an error response

We should then handle this differently, designing a code path that makes the error explicit rather than hiding this in a timestamp related error.

Copy link

@rluijk rluijk Dec 28, 2024

Choose a reason for hiding this comment

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

Just my two cents. The second use of response in _map_usage(response) is not explicitly validated. The validation check (response.created) occurs only inside _process_response. This means that _map_usage(response) operates on an unvalidated response, which could lead to unexpected issues if the response is invalid. (in the async def request).

Copy link

Choose a reason for hiding this comment

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

O, heck, I overlooked that the exception raised in _process_response ensures _map_usage cannot receive the response if it’s invalid. This guarantees the response is always validated before being passed further.

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.

[BUG] Error responses are not handled correctly for google openapi/openrouter
3 participants