-
Notifications
You must be signed in to change notification settings - Fork 4.2k
fix: tolerate partial LiteLLM citations #3655
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
Open
devin-lai
wants to merge
2
commits into
openai:main
Choose a base branch
from
devin-lai:fix/litellm-citation-annotations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
|
|
||
| from litellm.types.utils import Message | ||
|
|
||
| from agents.extensions.models.litellm_model import LitellmConverter | ||
|
|
||
|
|
||
| def _message_with_annotations(annotations: list[dict[str, Any]]) -> Message: | ||
| # Pass raw provider-shaped dicts intentionally; these mirror the partial payloads | ||
| # LiteLLM forwards and would not satisfy the strict ChatCompletionAnnotation type. | ||
| return Message.model_construct(role="assistant", content="hi", annotations=annotations) | ||
|
|
||
|
|
||
| def test_convert_annotations_returns_none_when_absent() -> None: | ||
| message = Message(role="assistant", content="hi") | ||
| assert LitellmConverter.convert_annotations_to_openai(message) is None | ||
|
|
||
|
|
||
| def test_convert_annotations_maps_full_citation() -> None: | ||
| message = _message_with_annotations( | ||
| [ | ||
| { | ||
| "type": "url_citation", | ||
| "url_citation": { | ||
| "start_index": 1, | ||
| "end_index": 4, | ||
| "url": "https://example.com", | ||
| "title": "Example", | ||
| }, | ||
| } | ||
| ] | ||
| ) | ||
| result = LitellmConverter.convert_annotations_to_openai(message) | ||
| assert result is not None | ||
| assert len(result) == 1 | ||
| citation = result[0].url_citation | ||
| assert citation.start_index == 1 | ||
| assert citation.end_index == 4 | ||
| assert citation.url == "https://example.com" | ||
| assert citation.title == "Example" | ||
|
|
||
|
|
||
| def test_convert_annotations_defaults_missing_title() -> None: | ||
| # Providers reached through LiteLLM may omit fields the OpenAI schema marks as | ||
| # required; hard indexing those fields previously raised KeyError and aborted | ||
| # the whole turn instead of degrading gracefully. | ||
| message = _message_with_annotations( | ||
| [ | ||
| { | ||
| "type": "url_citation", | ||
| "url_citation": { | ||
| "start_index": 0, | ||
| "end_index": 5, | ||
| "url": "https://example.com", | ||
| }, | ||
| } | ||
| ] | ||
| ) | ||
| result = LitellmConverter.convert_annotations_to_openai(message) | ||
| assert result is not None | ||
| assert len(result) == 1 | ||
| assert result[0].url_citation.url == "https://example.com" | ||
| assert result[0].url_citation.title == "" | ||
|
|
||
|
|
||
| def test_convert_annotations_defaults_missing_indices_and_title() -> None: | ||
| message = _message_with_annotations( | ||
| [ | ||
| { | ||
| "type": "url_citation", | ||
| "url_citation": { | ||
| "url": "https://example.com", | ||
| "title": None, | ||
| "start_index": None, | ||
| "end_index": None, | ||
| }, | ||
| } | ||
| ] | ||
| ) | ||
| result = LitellmConverter.convert_annotations_to_openai(message) | ||
| assert result is not None | ||
| assert len(result) == 1 | ||
| citation = result[0].url_citation | ||
| assert citation.start_index == 0 | ||
| assert citation.end_index == 0 | ||
| assert citation.url == "https://example.com" | ||
| assert citation.title == "" | ||
|
|
||
|
|
||
| def test_convert_annotations_skips_entries_without_url_citation_payload() -> None: | ||
| # LiteLLM enforces type == "url_citation" but allows the url_citation payload to be | ||
| # absent; such an entry carries no citation data, so it is skipped rather than | ||
| # emitted as an empty citation. | ||
| message = _message_with_annotations( | ||
| [ | ||
| {"type": "url_citation"}, | ||
| { | ||
| "type": "url_citation", | ||
| "url_citation": { | ||
| "start_index": 0, | ||
| "end_index": 2, | ||
| "url": "https://example.com", | ||
| "title": "Kept", | ||
| }, | ||
| }, | ||
| ] | ||
| ) | ||
| result = LitellmConverter.convert_annotations_to_openai(message) | ||
| assert result is not None | ||
| assert len(result) == 1 | ||
| assert result[0].url_citation.title == "Kept" | ||
|
|
||
|
|
||
| def test_convert_annotations_returns_none_when_no_usable_citations() -> None: | ||
| message = _message_with_annotations( | ||
| [ | ||
| {"type": "url_citation"}, | ||
| {"type": "url_citation", "url_citation": {"title": "Missing URL"}}, | ||
| ] | ||
| ) | ||
|
|
||
| assert LitellmConverter.convert_annotations_to_openai(message) is None |
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
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.
When LiteLLM returns a valid
message.annotationslist, this helper now buildsAnnotationobjects, but the non-streamingLitellmModel.get_responsepath immediately passes the message intoConverter.message_to_output_items, which still createsResponseOutputText(..., annotations=[])insrc/agents/models/chatcmpl_converter.py. As a result, users ofRunner.runstill receive empty annotations even though this converter accepted the citation, and the new tests miss that end-to-end path.Useful? React with 👍 / 👎.