fix(openai-agents): capture response.instructions as system prompt on generation spans#3739
Conversation
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed everything up to 9b3bdd2 in 12 seconds. Click for details.
- Reviewed
47lines of code in1files - Skipped
0files when reviewing. - Skipped posting
0draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
Workflow ID: wflow_ZgSQMc8t1OACSjmc
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: defaults Review profile: CHILL Plan: Pro Disabled knowledge base sources:
📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughPrepend Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py (1)
626-637: Deduplicate the instruction-prepend block to avoid behavior drift.The same logic is duplicated in both branches. A small helper keeps both paths consistent and easier to maintain.
Refactor proposal
@@ def _extract_response_attributes(otel_span, response, trace_content: bool): @@ return model_settings + + +def _prepend_system_instruction(input_data, response, trace_content: bool): + if not ( + trace_content + and response + and hasattr(response, "instructions") + and response.instructions + ): + return input_data + system_msg = {"role": "system", "content": response.instructions} + if not input_data: + return [system_msg] + return [system_msg] + (input_data if isinstance(input_data, list) else list(input_data)) @@ - if ( - trace_content - and response - and hasattr(response, "instructions") - and response.instructions - ): - system_msg = {"role": "system", "content": response.instructions} - input_data = [system_msg] + (input_data if input_data else []) + input_data = _prepend_system_instruction( + input_data, response, trace_content + ) @@ - if ( - trace_content - and response - and hasattr(response, "instructions") - and response.instructions - ): - system_msg = {"role": "system", "content": response.instructions} - input_data = [system_msg] + (input_data if input_data else []) + input_data = _prepend_system_instruction( + input_data, response, trace_content + )Also applies to: 687-695
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py` around lines 626 - 637, The duplicated block that prepends a system instruction to input_data (checking span_data.response.instructions and creating system_msg) should be extracted into a single helper function (e.g., _prepend_system_instructions(span_data, input_data) or _maybe_prepend_system_instructions) and invoked from both places in _hooks.py where the logic currently appears (the block around the response = getattr(span_data, "response", None) and the duplicate at 687-695); implement the helper to return the potentially modified input_data, handle None input_data correctly, and update the two call sites (instead of duplicating the condition and list construction) so behavior remains identical and easier to maintain.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In
`@packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py`:
- Around line 626-637: The duplicated block that prepends a system instruction
to input_data (checking span_data.response.instructions and creating system_msg)
should be extracted into a single helper function (e.g.,
_prepend_system_instructions(span_data, input_data) or
_maybe_prepend_system_instructions) and invoked from both places in _hooks.py
where the logic currently appears (the block around the response =
getattr(span_data, "response", None) and the duplicate at 687-695); implement
the helper to return the potentially modified input_data, handle None input_data
correctly, and update the two call sites (instead of duplicating the condition
and list construction) so behavior remains identical and easier to maintain.
ℹ️ Review info
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/opentelemetry-instrumentation-openai-agents/opentelemetry/instrumentation/openai_agents/_hooks.py
… generation spans
9b3bdd2 to
4f2e321
Compare
Fixes #3738
Problem
The
opentelemetry-instrumentation-openai-agentspackage does not captureresponse.instructionsfrom the OpenAI Responses API. When an agent hasinstructionsset (the system prompt), generation spans have nogen_ai.promptentry withrole: system— the system prompt is silently dropped.The
_extract_response_attributes()function readstemperature,max_output_tokens,top_p,model,frequency_penalty,output, andusage— but neverresponse.instructions.Fix
In
on_span_end, when handlingGenerationSpanData/ResponseSpanData, prependresponse.instructionsas a system message to the input data before calling_extract_prompt_attributes. This produces:gen_ai.prompt.0.role="system"gen_ai.prompt.0.content=<agent instructions>Followed by the conversation history at indices 1, 2, 3, etc.
Applied to both the primary code path (ResponseSpanData / GenerationSpanData) and the legacy fallback path.
Reference
This matches exactly how the vanilla
opentelemetry-instrumentation-openaipackage handles instructions inresponses_wrappers.py:Testing
Tested with an OpenAI Agents SDK agent that has
instructionsset. Before: no system prompt in generation spans. After: system prompt appears asgen_ai.prompt.0with rolesystem.Important
Capture
response.instructionsas a system prompt in generation spans inon_span_endin_hooks.py.on_span_endin_hooks.py, prependresponse.instructionsas a system message to input data forGenerationSpanDataandResponseSpanData.gen_ai.prompt.0.roleto"system"andgen_ai.prompt.0.contentto<agent instructions>.opentelemetry-instrumentation-openaiin handling instructions.instructionsset; system prompt now appears in generation spans.This description was created by
for 9b3bdd2. You can customize this summary. It will automatically update as commits are pushed.
Summary by CodeRabbit