-
Notifications
You must be signed in to change notification settings - Fork 565
fix(ai): add message truncation to openai–agents #4968
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
shellmayr
wants to merge
2
commits into
master
Choose a base branch
from
shellmayr/fix/gen-ai-message-truncation-openai-agents
base: master
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.
+114
−23
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
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 |
|---|---|---|
| @@ -1,33 +1,33 @@ | ||
| import asyncio | ||
| import json | ||
| import os | ||
| import re | ||
| import pytest | ||
| from unittest.mock import MagicMock, patch | ||
| import os | ||
|
|
||
| from sentry_sdk.integrations.openai_agents import OpenAIAgentsIntegration | ||
| from sentry_sdk.integrations.openai_agents.utils import safe_serialize | ||
| from sentry_sdk.utils import parse_version | ||
|
|
||
| import agents | ||
| import pytest | ||
| from agents import ( | ||
| Agent, | ||
| ModelResponse, | ||
| Usage, | ||
| ModelSettings, | ||
| Usage, | ||
| ) | ||
| from agents.items import ( | ||
| McpCall, | ||
| ResponseFunctionToolCall, | ||
| ResponseOutputMessage, | ||
| ResponseOutputText, | ||
| ResponseFunctionToolCall, | ||
| ) | ||
| from agents.version import __version__ as OPENAI_AGENTS_VERSION | ||
|
|
||
| from openai.types.responses.response_usage import ( | ||
| InputTokensDetails, | ||
| OutputTokensDetails, | ||
| ) | ||
|
|
||
| from sentry_sdk.integrations.openai_agents import OpenAIAgentsIntegration | ||
| from sentry_sdk.integrations.openai_agents.utils import safe_serialize | ||
| from sentry_sdk.utils import parse_version | ||
|
|
||
| test_run_config = agents.RunConfig(tracing_disabled=True) | ||
|
|
||
|
|
||
|
|
@@ -1051,8 +1051,8 @@ def test_openai_agents_message_role_mapping(sentry_init, capture_events): | |
|
|
||
| get_response_kwargs = {"input": test_input} | ||
|
|
||
| from sentry_sdk.integrations.openai_agents.utils import _set_input_data | ||
| from sentry_sdk import start_span | ||
| from sentry_sdk.integrations.openai_agents.utils import _set_input_data | ||
|
|
||
| with start_span(op="test") as span: | ||
| _set_input_data(span, get_response_kwargs) | ||
|
|
@@ -1061,8 +1061,6 @@ def test_openai_agents_message_role_mapping(sentry_init, capture_events): | |
| from sentry_sdk.consts import SPANDATA | ||
|
|
||
| if SPANDATA.GEN_AI_REQUEST_MESSAGES in span._data: | ||
| import json | ||
|
|
||
| stored_messages = json.loads(span._data[SPANDATA.GEN_AI_REQUEST_MESSAGES]) | ||
|
|
||
| # Verify roles were properly mapped | ||
|
|
@@ -1077,3 +1075,83 @@ def test_openai_agents_message_role_mapping(sentry_init, capture_events): | |
| # Verify no "ai" roles remain in any message | ||
| for message in stored_messages: | ||
| assert message["role"] != "ai" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_openai_agents_message_truncation( | ||
| sentry_init, capture_events, test_agent, mock_usage | ||
| ): | ||
| """Test that large messages are truncated properly in OpenAI Agents integration.""" | ||
| with patch.dict(os.environ, {"OPENAI_API_KEY": "test-key"}): | ||
| with patch( | ||
| "agents.models.openai_responses.OpenAIResponsesModel.get_response" | ||
| ) as mock_get_response: | ||
| large_content = ( | ||
| "This is a very long message that will exceed our size limits. " * 1000 | ||
| ) | ||
|
|
||
| large_response = ModelResponse( | ||
| output=[ | ||
| ResponseOutputMessage( | ||
| id="msg_large", | ||
| type="message", | ||
| status="completed", | ||
| content=[ | ||
| ResponseOutputText( | ||
| text=large_content, | ||
| type="output_text", | ||
| annotations=[], | ||
| ) | ||
| ], | ||
| role="assistant", | ||
| ) | ||
| ], | ||
| usage=mock_usage, | ||
| response_id="resp_large", | ||
| ) | ||
|
|
||
| mock_get_response.return_value = large_response | ||
|
|
||
| sentry_init( | ||
| integrations=[OpenAIAgentsIntegration()], | ||
| traces_sample_rate=1.0, | ||
| send_default_pii=True, | ||
| ) | ||
|
|
||
| events = capture_events() | ||
|
|
||
| # Create messages with mixed large/small content by patching get_response | ||
| with patch( | ||
| "agents.models.openai_responses.OpenAIResponsesModel.get_response" | ||
| ) as mock_inner: | ||
| mock_inner.side_effect = [large_response] * 5 | ||
|
|
||
| # We'll test with the agent itself, not the messages | ||
| # since OpenAI agents tracks messages internally | ||
| result = await agents.Runner.run( | ||
| test_agent, "Test input", run_config=test_run_config | ||
| ) | ||
|
|
||
| assert result is not None | ||
|
|
||
| assert len(events) > 0 | ||
| tx = events[0] | ||
| assert tx["type"] == "transaction" | ||
|
|
||
| # Check ai_client spans (these have the truncation) | ||
| ai_client_spans = [ | ||
| span for span in tx.get("spans", []) if span.get("op") == "gen_ai.chat" | ||
| ] | ||
| assert len(ai_client_spans) > 0 | ||
|
|
||
| # Just verify that messages are being set and truncation is applied | ||
| # The actual truncation behavior is tested in the ai_monitoring tests | ||
| ai_client_span = ai_client_spans[0] | ||
| if "gen_ai.request.messages" in ai_client_span["data"]: | ||
| messages_data = ai_client_span["data"]["gen_ai.request.messages"] | ||
| assert isinstance(messages_data, str) | ||
|
|
||
| parsed_messages = json.loads(messages_data) | ||
| assert isinstance(parsed_messages, list) | ||
| # Verify messages were processed | ||
| assert len(parsed_messages) >= 1 | ||
|
Comment on lines
+1147
to
+1157
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. Is this checking that truncation is applied? I would have thought |
||
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.
Bug: Missing Data Normalization in Agent Span
The
invoke_agent_spanfunction is missing a call to_normalize_data()before passingnormalized_messagestotruncate_and_annotate_messages(). This differs fromutils.pyand means the truncation function receives Python objects instead of the expected serialized data, preventing it from working correctly._normalize_dataalso needs to be imported.