Skip to content

Fix OpenAI Agents SDK guardrail span classification #1096

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
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

devin-ai-integration[bot]
Copy link
Contributor

Fix OpenAI Agents SDK guardrail span classification

Problem

The OpenAI Agents SDK integration was incorrectly classifying guardrail events as generic events or agent spans instead of properly mapping them to GUARDRAIL span types. This prevented proper observability and tracing of guardrail events in the AgentOps platform.

Root Cause

Two functions in the OpenAI Agents SDK instrumentation were missing handlers for GuardrailSpanData:

  1. get_span_kind() in exporter.py - didn't handle GuardrailSpanData, causing it to fall through to default SpanKind.INTERNAL
  2. get_span_attributes() in attributes/common.py - didn't have a case for GuardrailSpanData, so these spans didn't get proper AgentOps span kind attributes

Solution

Added proper GuardrailSpanData handling following the established patterns in the codebase:

Changes Made

  1. Updated get_span_kind() function in exporter.py:

    • Added GuardrailSpanData to the HandoffSpanData case for SpanKind.INTERNAL
  2. Added GUARDRAIL_SPAN_ATTRIBUTES mapping in attributes/common.py:

    • Consistent attribute extraction pattern for guardrail spans
  3. Added get_guardrail_span_attributes() function in attributes/common.py:

    • Extracts attributes from GuardrailSpanData objects
    • Sets proper AGENTOPS_SPAN_KIND to GUARDRAIL value
    • Handles guardrail-specific attributes like name and triggered
  4. Updated get_span_attributes() dispatcher in attributes/common.py:

    • Added GuardrailSpanData case to route to the new handler function

Testing

Verified with OpenAI Agents SDK guardrail example:

  • Ran examples/openai_agents/agent_guardrails.py
  • Successfully executed with "Math homework guardrail tripped" message
  • AgentOps session links generated, confirming instrumentation is active

Lint checks: Pre-commit hooks passed with automatic formatting applied

Impact

  • Guardrail events from OpenAI Agents SDK are now properly classified as GUARDRAIL spans
  • Maintains existing functionality for other span types (Agent, Function, Generation, etc.)
  • Follows established codebase patterns for consistency
  • Enables proper observability and tracing of guardrail events in AgentOps

Link to Devin run

https://app.devin.ai/sessions/0764439e61a6472fa04739fd1db8fb08

Requested by: Alex ([email protected])

- Add GuardrailSpanData handling to get_span_kind() function
- Add get_guardrail_span_attributes() function with proper GUARDRAIL span kind
- Add GUARDRAIL_SPAN_ATTRIBUTES mapping for consistent attribute extraction
- Add GuardrailSpanData case to get_span_attributes() dispatcher

Fixes issue where GuardrailSpanData spans were falling through to generic
classification instead of being properly mapped to GUARDRAIL span type.

Co-Authored-By: Alex <[email protected]>
Copy link

codecov bot commented Jun 20, 2025

Codecov Report

Attention: Patch coverage is 85.71429% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../instrumentation/agentic/openai_agents/exporter.py 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@areibman areibman requested a review from fenilfaldu June 20, 2025 06:41
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@areibman
Copy link
Contributor

Vibe coded but looks good? Can you test @fenilfaldu ?

devin-ai-integration bot and others added 2 commits June 20, 2025 06:50
- Add test_guardrail_span_attributes() to test main functionality
- Add test_guardrail_span_attributes_without_optional_fields() for edge cases
- Add GuardrailSpanData test case to test_span_attributes_dispatcher()
- Import get_guardrail_span_attributes in test imports

Addresses codecov/patch failure by adding comprehensive test coverage
for the new guardrail span handling code.

Co-Authored-By: Alex <[email protected]>
Apply automatic formatting changes from ruff-format to resolve
pre-commit check failure. Adds blank line after docstring.

Co-Authored-By: Alex <[email protected]>
Copy link
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants