#7783·crewAI

[BUG] OpenAI provider emits LLMCallFailedEvent twice per failed call and pops the enclosing agent scope

Author: breken-aiCreated Sep 26, 2026Updated Sep 26, 2026

Description

A failed non-streaming OpenAICompletion.call/acall (chat completions or the Responses API) emits LLMCallFailedEvent twice. _handle_completion/_ahandle_completion/_handle_responses/_ahandle_responses emit it and re-raise, and call/acall emit it again.

The second ending event pops whatever scope encloses the call off the event stack, so the console shows Event pairing mismatch. 'llm_call_failed' closed 'agent_execution_started' and later events get the wrong parents. A responses-only model (e.g. gpt-5-pro) that the Responses API fallback recovers also reports a failed call even though the call succeeds.

Anthropic, Gemini, Azure and Bedrock emit the failure only in call/acall.

Steps to Reproduce

  1. Create OpenAICompletion(model="gpt-4o", api_key="sk-test") whose client uses an httpx.MockTransport that answers every request with HTTP 400.
  2. Subscribe to LLMCallFailedEvent on crewai_event_bus.
  3. Inside event_scope(...) for an agent_execution_started event, call llm.call([{"role": "user", "content": "hi"}]) and catch the error.
  4. Count the failure events and inspect the event scope stack.

Expected behavior

One LLMCallFailedEvent per failed call, and the enclosing agent_execution_started scope still on the stack afterwards. A call that the Responses API fallback recovers should report no failure.

Screenshots/Code snippets

Duplicate emission: lib/crewai/src/crewai/llms/providers/openai/completion.py, the four non-streaming handlers each call self._emit_call_failed_event(...) before re-raising, and call/acall emit again in their own except block.

Operating System

Other (specify in additional context)

Python Version

3.13 (not in the dropdown)

crewAI Version

1.15.22 (main @ 4ed2abc7)

crewAI Tools Version

N/A (not involved)

Virtual Environment

Venv

Evidence

With the mock transport above on main @ 4ed2abc7, all five cases misbehave:

  • 400 on call and acall, for both chat completions and the Responses API: 2 LLMCallFailedEvents, and the enclosing agent_execution_started scope is popped.
  • Responses-only 404 recovered by the fallback: 1 LLMCallFailedEvent although the call returns done.

Present since v1.0.0 (d1343b96).

Possible Solution

Remove the emissions from the four handlers so call/acall report the failure once, like the other native providers. Exception conversion and logging stay as they are. I have a fix with regression tests ready and will open a PR linked to this issue.

Additional context

Reproduced on macOS 26.5 with the uv workspace venv (Python 3.13.12).

This issue was written by an AI coding agent (Claude Code) on the breken-ai account. Per CONTRIBUTING it should carry the llm-generated label; if this account cannot apply it, please add it.