Skip to content

Fix ResponseTextDeltaEvent validation error for non-OpenAI model providers #1226

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 4 commits into
base: main
Choose a base branch
from

Conversation

vmalyi
Copy link

@vmalyi vmalyi commented Jul 23, 2025

The ChatCmplStreamHandler.handle_stream() method fails with a Pydantic validation error when creating ResponseTextDeltaEvent objects during streaming operations with non-OpenAI model providers (e.g., Gemini via Google's OpenAI-compatible API).

Error:

pydantic_core._pydantic_core.ValidationError: 1 validation error for ResponseTextDeltaEvent
logprobs
  Field required [type=missing, input_value={'content_index': 0, 'del...', 'sequence_number': 3}, input_type=dict]

Root Cause:
The ResponseTextDeltaEvent constructor in chatcmpl_stream_handler.py:193 is missing the required logprobs parameter. The OpenAI SDK type definition for ResponseTextDeltaEvent defines logprobs: List[Logprob] as a required field, but the Agents SDK doesn't provide it when creating these events.

Type Inconsistency:

  • ResponseTextDeltaEvent.logprobs: List[Logprob] (required)
  • ResponseOutputText.logprobs: Optional[List[Logprob]] = None (optional)

This inconsistency causes issues when using model providers that don't return logprobs data.

Debug information

  • Agents SDK version: v0.2.3
  • Python version: Python 3.10
  • OpenAI SDK version: 1.97.1
  • Pydantic version: 2.11.7

Repro steps

import asyncio
from agents import Agent, Runner
from agents.models.openai_chatcompletions import OpenAIChatCompletionsModel
from openai import AsyncOpenAI

async def reproduce_bug():
    # Using Gemini via OpenAI-compatible API (doesn't provide logprobs)
    gemini_client = AsyncOpenAI(
        base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
        api_key="your-gemini-api-key",
    )
    
    agent = Agent(
        name="Test Agent",
        instructions="You are a helpful assistant.",
        model=OpenAIChatCompletionsModel(
            model="gemini-2.5-pro",
            openai_client=gemini_client,
        ),
    )
    
    # This will fail with the logprobs validation error
    stream_result = Runner.run_streamed(agent, "Hello, world!")
    
    async for event in stream_result.stream_events():
        if event.type == "raw_response_event":
            continue  # Error occurs here during internal stream processing

if __name__ == "__main__":
    asyncio.run(reproduce_bug())

The error occurs in agents/models/chatcmpl_stream_handler.py at line 193 where ResponseTextDeltaEvent is constructed without the required logprobs field.

Expected behavior

Streaming should work seamlessly with all OpenAI-compatible model providers, including those that don't provide logprobs data (like Gemini). The ResponseTextDeltaEvent should be created with proper logprobs handling that:

  1. Uses actual logprobs data when available (OpenAI models)
  2. Falls back to empty list when not available (non-OpenAI providers)

Solution

The fix uses getattr() to gracefully handle the logprobs field, following the existing pattern used elsewhere in the codebase (like line 91 with chunk.usage):

Before:

yield ResponseTextDeltaEvent(
    content_index=state.text_content_index_and_output[0],
    delta=delta.content,
    item_id=FAKE_RESPONSES_ID,
    # logprobs field missing - causes validation error
    output_index=state.reasoning_content_index_and_output
    is not None,
    type="response.output_text.delta",
    sequence_number=sequence_number.get_and_increment(),
)

After:

yield ResponseTextDeltaEvent(
    content_index=state.text_content_index_and_output[0],
    delta=delta.content,
    item_id=FAKE_RESPONSES_ID,
    logprobs=getattr(delta, "logprobs", []) or [],
    output_index=state.reasoning_content_index_and_output
    is not None,
    type="response.output_text.delta",
    sequence_number=sequence_number.get_and_increment(),
)

Impact

This bug affects:

  • All users of non-OpenAI model providers (e.g. Gemini)
  • Any streaming operations with models that don't return logprobs
  • All official SDK examples when used with non-OpenAI providers
  • Integration with LiteLLM and other OpenAI-compatible proxy services

Testing

I've tested the fix with:

  • ✅ Gemini 2.5 Pro via Google's OpenAI-compatible API
  • ✅ Streaming operations with Runner.run_streamed()
  • ✅ Both raw_response_event and run_item_stream_event handling
  • ✅ Backward compatibility with OpenAI models (when logprobs are provided)

…iders

The `ChatCmplStreamHandler.handle_stream()` method fails with a Pydantic validation error when creating `ResponseTextDeltaEvent` objects during streaming operations with non-OpenAI model providers (e.g., Gemini via Google's OpenAI-compatible API).

Streaming should work seamlessly with all OpenAI-compatible model providers, including those that don't provide logprobs data (like Gemini). The `ResponseTextDeltaEvent` should be created with proper logprobs handling that:

1. Uses actual logprobs data when available (OpenAI models)
2. Falls back to empty list when not available (non-OpenAI providers)
3. Maintains backward compatibility
@vmalyi
Copy link
Author

vmalyi commented Jul 23, 2025

Most likely caused by #715

@parthushah8
Copy link

an easy fix for me was to downgrade to OpenAI SDK version: 1.97.0

@handrew
Copy link
Contributor

handrew commented Jul 25, 2025

Also getting this issue. Switching to openai==1.96.1 works but unideal - hope this gets merged soon. Hopefully a small change, 🙏🏼 @rm-openai

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

Successfully merging this pull request may close these issues.

4 participants