-
Notifications
You must be signed in to change notification settings - Fork 616
feat(openai-agents): Support span streaming #6404
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
alexander-alderman-webb
wants to merge
17
commits into
master
Choose a base branch
from
webb/openai-agents/span-first-2
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.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6f67df0
fix(openai-agents): Remove redundant hosted MCP tool spans
alexander-alderman-webb 0f53ac0
feat(openai-agents): Support span streaming
alexander-alderman-webb 5a5f81d
.
alexander-alderman-webb c4a5366
mypy
alexander-alderman-webb b2b0c34
mypy2
alexander-alderman-webb bf37fa2
merge master
alexander-alderman-webb 53a84ae
handle streamed span invoke agent
alexander-alderman-webb dcdeb8d
truncate invoke agent attributes
alexander-alderman-webb 2c1bd19
fix bool logic
alexander-alderman-webb e9413c5
merge master
alexander-alderman-webb e243b86
add brackets to bool logic
alexander-alderman-webb 931aa30
drop transaction reference in span streaming comment
alexander-alderman-webb 3a95520
test(openai-agents): Deduplicate in tests by removing node.callspec.i…
alexander-alderman-webb ab65aa4
Merge branch 'webb/openai-agents/remove-node-callspec' into webb/open…
alexander-alderman-webb e1d6ebe
merge cleanup
alexander-alderman-webb 0b4ed5f
add bool precedence
alexander-alderman-webb f941351
merge master
alexander-alderman-webb 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
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 |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.consts import OP, SPANDATA | ||
| from sentry_sdk.traces import StreamedSpan | ||
| from sentry_sdk.tracing_utils import has_span_streaming_enabled | ||
|
|
||
| from ..consts import SPAN_ORIGIN | ||
| from ..utils import ( | ||
|
|
@@ -12,14 +14,14 @@ | |
| ) | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Any, Optional | ||
| from typing import Any, Optional, Union | ||
|
|
||
| from agents import Agent | ||
|
|
||
|
|
||
| def ai_client_span( | ||
| agent: "Agent", get_response_kwargs: "dict[str, Any]" | ||
| ) -> "sentry_sdk.tracing.Span": | ||
| ) -> "Union[sentry_sdk.tracing.Span, StreamedSpan]": | ||
| # TODO-anton: implement other types of operations. Now "chat" is hardcoded. | ||
| # Get model name from agent.model or fall back to request model (for when agent.model is None/default) | ||
| model_name = None | ||
|
|
@@ -28,13 +30,24 @@ def ai_client_span( | |
| elif hasattr(agent, "_sentry_request_model"): | ||
| model_name = agent._sentry_request_model | ||
|
|
||
| span = sentry_sdk.start_span( | ||
| op=OP.GEN_AI_CHAT, | ||
| name=f"chat {model_name}", | ||
| origin=SPAN_ORIGIN, | ||
| ) | ||
| # TODO-anton: remove hardcoded stuff and replace something that also works for embedding and so on | ||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | ||
| span_streaming = has_span_streaming_enabled(sentry_sdk.get_client().options) | ||
| if span_streaming: | ||
| span = sentry_sdk.traces.start_span( | ||
| name=f"chat {model_name}", | ||
|
alexander-alderman-webb marked this conversation as resolved.
|
||
| attributes={ | ||
| "sentry.op": OP.GEN_AI_CHAT, | ||
| "sentry.origin": SPAN_ORIGIN, | ||
| SPANDATA.GEN_AI_OPERATION_NAME: "chat", | ||
| }, | ||
| ) | ||
| else: | ||
| span = sentry_sdk.start_span( | ||
| op=OP.GEN_AI_CHAT, | ||
| name=f"chat {model_name}", | ||
| origin=SPAN_ORIGIN, | ||
| ) | ||
| # TODO-anton: remove hardcoded stuff and replace something that also works for embedding and so on | ||
|
Member
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. I'm not sure how useful this comment is anymore - any chance we can remove it?
Contributor
Author
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. I think this is still a bug 😬. |
||
| span.set_data(SPANDATA.GEN_AI_OPERATION_NAME, "chat") | ||
|
|
||
| _set_agent_data(span, agent) | ||
| _set_input_data(span, get_response_kwargs) | ||
|
|
@@ -43,7 +56,7 @@ def ai_client_span( | |
|
|
||
|
|
||
| def update_ai_client_span( | ||
| span: "sentry_sdk.tracing.Span", | ||
| span: "Union[sentry_sdk.tracing.Span, StreamedSpan]", | ||
| response: "Any", | ||
| response_model: "Optional[str]" = None, | ||
| agent: "Optional[Agent]" = None, | ||
|
|
@@ -55,13 +68,17 @@ def update_ai_client_span( | |
| if hasattr(response, "output") and response.output: | ||
| _set_output_data(span, response) | ||
|
|
||
| set_on_span = ( | ||
| span.set_attribute if isinstance(span, StreamedSpan) else span.set_data | ||
| ) | ||
|
|
||
| if response_model is not None: | ||
| span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) | ||
| set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) | ||
| elif hasattr(response, "model") and response.model: | ||
| span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, str(response.model)) | ||
| set_on_span(SPANDATA.GEN_AI_RESPONSE_MODEL, str(response.model)) | ||
|
|
||
| # Set conversation ID from agent if available | ||
| if agent: | ||
| conv_id = getattr(agent, "_sentry_conversation_id", None) | ||
| if conv_id: | ||
| span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id) | ||
| set_on_span(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id) | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.