Skip to content

'AsyncOpenAI' Object Has No Attribute 'responses' in Async Runner & 'this event loop is already running' in Sync Runner #542

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

Closed
RocketDonald opened this issue Apr 18, 2025 · 3 comments
Labels
bug Something isn't working needs-more-info Waiting for a reply/more info from the author stale

Comments

@RocketDonald
Copy link

RocketDonald commented Apr 18, 2025

Describe the bug

I cannot run the agent as it keeps raising the 'AsyncOpenAI' Object Has No Attribute 'responses' Error. Moreover, if I run the agent with run_sync, an error message 'this event loop is already running' shows.

Debug information

  • Agents SDK version: v0.0.11 (Also happens with v0.0.9)
  • OpenAI version: v1.75.0 (Also tried downgrading to v1.66.5, same issue happened)
  • Python version v3.12.4

Repro steps

class AgentRequest(BaseModel):
    user_idea: str

@router.get("/begin_thinking", status_code=status.HTTP_200_OK)
async def begin_thinking(
    topics: AgentRequest,
) -> JSONResponse:
    try:
        try:
            agent = Agent(
                name = "Testing Agent",
                instructions="You are a cat, so you need to think like a cat and add cat emojis to your response."
            )
        except Exception as e:
            raise Exception(f"Error in the agent creation: {e}")
        
        
        logger.debug(f"Beginning thinking with topics: {topics.user_idea}")
        
        try:
            result = Runner.run_sync(
                starting_agent=agent, input=topics.user_idea
            )
        except Exception as e:
            raise Exception(f"Error in the agent run: {e}")
        
        if not result:
            raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="No results found")
        
        return JSONResponse(
            content={
                "status": "success",
                "data": result.final_output,
            },
            status_code=status.HTTP_200_OK,
        )
       
    except Exception as e:
        logger.error(f"Error in running the agent: {e}")
        raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Something went wrong while running the agent. {e}")

Logger Record When I use Runner.run()

DAOS DEBUG LOGS: 2025-04-18 08:48:18,133 - openai.agents - DEBUG - Creating trace Agent workflow with id trace_f73ee56d23f74f098d7990c3cdfc9336
DAOS DEBUG LOGS: 2025-04-18 08:48:18,133 - openai.agents - DEBUG - Setting current trace: trace_f73ee56d23f74f098d7990c3cdfc9336
DAOS DEBUG LOGS: 2025-04-18 08:48:18,133 - openai.agents - DEBUG - Creating span <agents.tracing.span_data.AgentSpanData object at 0xfffefbb6cf40> with id None
DAOS DEBUG LOGS: 2025-04-18 08:48:18,133 - openai.agents - DEBUG - Running agent Testing Agent (turn 1)
DAOS DEBUG LOGS: 2025-04-18 08:48:18,137 - openai.agents - DEBUG - Creating span <agents.tracing.span_data.ResponseSpanData object at 0xfffefbf991c0> with id None
DAOS DEBUG LOGS: 2025-04-18 08:48:18,138 - openai.agents - DEBUG - Calling LLM gpt-4o with input:
[
{
"content": "The history of train",
"role": "user"
}
]
Tools:
[]
Stream: False
Tool choice: NOT_GIVEN
Response format: NOT_GIVEN
Previous response id: None

DAOS DEBUG LOGS: 2025-04-18 08:48:18,138 - openai.agents - ERROR - Error getting response: 'AsyncOpenAI' object has no attribute 'responses'. (request_id: None)
DAOS DEBUG LOGS: 2025-04-18 08:48:18,138 - openai.agents - DEBUG - Resetting current trace
DAOS DEBUG LOGS: 2025-04-18 08:48:18,139 - asyncio - ERROR - Task exception was never retrieved
future: <Task finished name='Task-4' coro=<Runner.run() done, defined at /usr/local/lib/python3.10/site-packages/agents/run.py:110> exception=AttributeError("'AsyncOpenAI' object has no attribute 'responses'")>
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/agents/run.py", line 218, in run
input_guardrail_results, turn_result = await asyncio.gather(
File "/usr/local/lib/python3.10/site-packages/agents/run.py", line 756, in _run_single_turn
new_response = await cls._get_new_response(
File "/usr/local/lib/python3.10/site-packages/agents/run.py", line 915, in _get_new_response
new_response = await model.get_response(
File "/usr/local/lib/python3.10/site-packages/agents/models/openai_responses.py", line 76, in get_response
response = await self._fetch_response(
File "/usr/local/lib/python3.10/site-packages/agents/models/openai_responses.py", line 242, in _fetch_response
return await self._client.responses.create(
AttributeError: 'AsyncOpenAI' object has no attribute 'responses'
DAOS DEBUG LOGS: 2025-04-18 08:48:18,705 - httpcore.connection - DEBUG - connect_tcp.started host='api.openai.com' port=443 local_address=None timeout=5.0 socket_options=None
DAOS DEBUG LOGS: 2025-04-18 08:48:18,749 - httpcore.connection - DEBUG - connect_tcp.complete return_value=<httpcore._backends.sync.SyncStream object at 0xfffefbb94430>
DAOS DEBUG LOGS: 2025-04-18 08:48:18,750 - httpcore.connection - DEBUG - start_tls.started ssl_context=<ssl.SSLContext object at 0xffff831baec0> server_hostname='api.openai.com' timeout=5.0
DAOS DEBUG LOGS: 2025-04-18 08:48:18,758 - httpcore.connection - DEBUG - start_tls.complete return_value=<httpcore._backends.sync.SyncStream object at 0xfffefbb94400>
DAOS DEBUG LOGS: 2025-04-18 08:48:19,333 - httpx - INFO - HTTP Request: POST https://api.openai.com/v1/traces/ingest "HTTP/1.1 204 No Content"
DAOS DEBUG LOGS: 2025-04-18 08:48:19,334 - openai.agents - DEBUG - Exported 3 items
DAOS DEBUG LOGS: 2025-04-18 08:52:21,951 - openai.agents - DEBUG - Shutting down trace provider
DAOS DEBUG LOGS: 2025-04-18 08:52:21,952 - openai.agents - DEBUG - Shutting down trace processor <agents.tracing.processors.BatchTraceProcessor object at 0xffff83245b40>

Logger Record When I use Runner.run_sync() instead of Runner.run()

DAOS DEBUG LOGS: 2025-04-18 08:48:18,132 - app.routers.agentic_workflow - ERROR - Error in running the agent: Error in the agent run: this event loop is already running.

Expected behavior

The agent should run in both sync and async mode.

@RocketDonald RocketDonald added the bug Something isn't working label Apr 18, 2025
@rm-openai
Copy link
Collaborator

Can you add this to the top of your code and share what it outputs:

import
print(f"oai version: {openai.__version__}")

Seems like you're somehow on an older openai SDK version

@rm-openai rm-openai added the needs-more-info Waiting for a reply/more info from the author label Apr 19, 2025
Copy link

This issue is stale because it has been open for 7 days with no activity.

@github-actions github-actions bot added the stale label Apr 27, 2025
Copy link

github-actions bot commented May 1, 2025

This issue was closed because it has been inactive for 3 days since being marked as stale.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale May 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-more-info Waiting for a reply/more info from the author stale
Projects
None yet
Development

No branches or pull requests

2 participants