-
Notifications
You must be signed in to change notification settings - Fork 579
feat(openai-agents): Set system instruction attribute on gen_ai.chat spans
#5370
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
Merged
alexander-alderman-webb
merged 42 commits into
master
from
webb/openai-agents-chat-system-instructions
Jan 28, 2026
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
a7990ad
ref(openai): Separate input handling to improve typing
alexander-alderman-webb c6ebc0f
remove old func
alexander-alderman-webb 8d9fa37
.
alexander-alderman-webb ab41f1e
expand type
alexander-alderman-webb b3117ac
add ignores
alexander-alderman-webb 847d4b2
revert unrelated change
alexander-alderman-webb 4f25a56
feat(openai): Set system instruction attribute
alexander-alderman-webb 903bbbd
use union
alexander-alderman-webb f304bec
mypy
alexander-alderman-webb 921a82f
Merge branch 'webb/openai-separate-input-handling' into webb/openai-a…
alexander-alderman-webb 9f5d801
.
alexander-alderman-webb d66ffe1
use specific openai types
alexander-alderman-webb ac3ce00
wip
alexander-alderman-webb ef9fe6f
.
alexander-alderman-webb ce84a29
.
alexander-alderman-webb dee9930
remove test
alexander-alderman-webb cb00ab3
.
alexander-alderman-webb 26b932b
.
alexander-alderman-webb 04dc92c
.
alexander-alderman-webb c7263ea
edge case
alexander-alderman-webb d947899
full responses api tests
alexander-alderman-webb 8cbeac1
remove sentry_sdk/ai/_openai_completions_api.py
alexander-alderman-webb bcebcc8
fix test
alexander-alderman-webb f749ae4
Merge branch 'master' into webb/openai-separate-input-handling
alexander-alderman-webb 87dd8fe
Merge branch 'webb/openai-separate-input-handling' into webb/openai-a…
alexander-alderman-webb 085b496
feat(openai-agents): Set system instruction attribute on gen_ai.chat …
alexander-alderman-webb 48c7fbe
add type ignores
alexander-alderman-webb 3f5ad11
more defensive checks in case input is not iterable
alexander-alderman-webb ebf2d9c
merge
alexander-alderman-webb fc9f1fa
pick up changes to extraction functions
alexander-alderman-webb bcdd87c
fix func name
alexander-alderman-webb 68b853f
fix Iterable import
alexander-alderman-webb 5ee5274
remove runtime import
alexander-alderman-webb a8840d5
more early returns
alexander-alderman-webb 382e933
revert unrelated tests
alexander-alderman-webb 179d59e
revert unrelated change
alexander-alderman-webb 12cb219
address comment
alexander-alderman-webb a6152fe
remove unused type ignore
alexander-alderman-webb 74b5a4c
merge
alexander-alderman-webb eda980c
fix typo in filename
alexander-alderman-webb 5825835
remove unused import
alexander-alderman-webb c836563
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| from collections.abc import Iterable | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from sentry_sdk._types import TextPart | ||
|
|
||
| from openai.types.chat import ( | ||
| ChatCompletionMessageParam, | ||
| ChatCompletionSystemMessageParam, | ||
| ) | ||
|
|
||
|
|
||
| def _is_system_instruction(message: "ChatCompletionMessageParam") -> bool: | ||
| return isinstance(message, dict) and message.get("role") == "system" | ||
|
|
||
|
|
||
| def _get_system_instructions( | ||
| messages: "Iterable[ChatCompletionMessageParam]", | ||
| ) -> "list[ChatCompletionMessageParam]": | ||
| if not isinstance(messages, Iterable): | ||
| return [] | ||
|
|
||
| return [message for message in messages if _is_system_instruction(message)] | ||
|
|
||
|
|
||
| def _transform_system_instructions( | ||
| system_instructions: "list[ChatCompletionSystemMessageParam]", | ||
| ) -> "list[TextPart]": | ||
| instruction_text_parts: "list[TextPart]" = [] | ||
|
|
||
| for instruction in system_instructions: | ||
| if not isinstance(instruction, dict): | ||
| continue | ||
|
|
||
| content = instruction.get("content") | ||
|
|
||
| if isinstance(content, str): | ||
| instruction_text_parts.append({"type": "text", "content": content}) | ||
|
|
||
| elif isinstance(content, list): | ||
| for part in content: | ||
| if isinstance(part, dict) and part.get("type") == "text": | ||
| text = part.get("text", None) | ||
| if text is not None: | ||
| instruction_text_parts.append({"type": "text", "content": text}) | ||
|
|
||
| return instruction_text_parts | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,22 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Union | ||
|
|
||
| from openai.types.responses import ResponseInputParam, ResponseInputItemParam | ||
|
|
||
|
|
||
| def _is_system_instruction(message: "ResponseInputItemParam") -> bool: | ||
| if not isinstance(message, dict) or not message.get("role") == "system": | ||
| return False | ||
|
|
||
| return "type" not in message or message["type"] == "message" | ||
|
|
||
|
|
||
| def _get_system_instructions( | ||
| messages: "Union[str, ResponseInputParam]", | ||
| ) -> "list[ResponseInputItemParam]": | ||
| if not isinstance(messages, list): | ||
| return [] | ||
|
|
||
| return [message for message in messages if _is_system_instruction(message)] |
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
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.