Skip to content

Conversation

@devin-ai-integration
Copy link
Contributor

@devin-ai-integration devin-ai-integration bot commented Jun 22, 2025

Fix OpenAI Stream Wrapper Attribute Delegation

Fixes #1101

Problem

Users integrating AgentOps with ChainLit and other frameworks were encountering the error:

'OpenAIAsyncStreamWrapper' object has no attribute 'choices'

This occurred when code tried to access attributes like choices on OpenAI streaming responses that were wrapped by AgentOps instrumentation.

Root Cause

The OpenAIAsyncStreamWrapper and OpenaiStreamWrapper classes act as proxies around OpenAI stream objects but didn't implement __getattr__ to delegate attribute access to the underlying stream. When code accessed stream.choices, Python looked for the attribute on the wrapper class, couldn't find it, and raised an AttributeError.

Solution

Added __getattr__ method to both wrapper classes following the established pattern in the codebase (similar to agentops/instrumentation/agentic/agno/instrumentor.py):

def __getattr__(self, name):
    """Delegate attribute access to the original stream."""
    return getattr(self._stream, name)

This ensures any attribute not explicitly defined on the wrapper gets forwarded to the underlying stream object, making the wrapper transparent to users.

Changes

  • Added __getattr__ method to OpenAIAsyncStreamWrapper class
  • Added __getattr__ method to OpenaiStreamWrapper class (sync version) for consistency
  • Both methods delegate attribute access to self._stream

Testing

  • Created and ran attribute delegation test to verify the pattern works correctly
  • Verified the fix handles choices, model, id, and other stream attributes
  • Changes are minimal and focused only on the specific issue

Compatibility

  • No breaking changes - this only adds missing functionality
  • Maintains all existing wrapper behavior for instrumentation
  • Works with ChainLit, AsyncOpenAI, and other integrations that access stream attributes

Link to Devin run: https://app.devin.ai/sessions/37821e78215c47a2965b384be6169155

Requested by: Alex ([email protected])

- Add __getattr__ method to OpenAIAsyncStreamWrapper and OpenaiStreamWrapper
- Fixes 'choices' attribute error when accessing stream attributes
- Follows existing pattern from agno instrumentor for attribute delegation
- Enables transparent access to underlying stream properties like choices, model, id

Co-Authored-By: Alex <[email protected]>
@devin-ai-integration
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

@codecov
Copy link

codecov bot commented Jun 22, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

devin-ai-integration bot and others added 2 commits June 22, 2025 18:28
- Test __getattr__ method in both OpenaiStreamWrapper and OpenAIAsyncStreamWrapper
- Verify proper delegation of choices, model, id, and usage attributes
- Test AttributeError handling for missing attributes
- Addresses codecov/patch coverage requirement for PR #1098

Co-Authored-By: Alex <[email protected]>
- Consolidate multi-line imports to single line
- Remove trailing whitespace
- Format list definitions for consistency
- Addresses pre-commit checks failure in PR #1098

Co-Authored-By: Alex <[email protected]>
@areibman areibman closed this Jul 1, 2025
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.

OpenAI Stream Wrapper Missing Attribute Delegation Causes AttributeError

4 participants