Skip to content

Conversation

@seratch
Copy link
Member

@seratch seratch commented Jan 12, 2026

This pull request fixes LiteLLM Pydantic serializer warnings during streaming by adding an opt-in patch in src/agents/extensions/models/litellm_model.py that wraps LiteLLM’s private logging helper to serialize BaseModel instances with warnings disabled, and by adding a unit test in tests/models/test_litellm_logging_patch.py to assert the env var controls whether the patch is applied. It documents the LiteLLM issue context and the private-helper dependency risk, with guidance to remove the patch once LiteLLM resolves the underlying issue. This pull request resolves #2291.

Test script:

import asyncio

from agents import Agent, ModelSettings, RunConfig, Runner


async def main() -> int:
    """Stream events from a Claude model via LiteLLM to validate warning suppression."""
    agent = Agent(
        name="claude-streaming-repro",
        model="litellm/anthropic/claude-sonnet-4-5-20250929",
        instructions="You are a helpful assistant. Keep responses brief.",
        model_settings=ModelSettings(temperature=0.7),
    )

    run_config = RunConfig(tracing_disabled=True)

    result = Runner.run_streamed(
        agent,
        input=[{"role": "user", "content": "Say hello in one sentence."}],
        run_config=run_config,
    )

    event_count = 0
    async for event in result.stream_events():
        event_count += 1
        print(f"Event {event_count}: {event.__class__.__name__}")

    print(f"Total events: {event_count}")
    return event_count


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

before:

$ uv run python test.py
Event 1: AgentUpdatedStreamEvent
Event 2: RawResponsesStreamEvent
Event 3: RawResponsesStreamEvent
Event 4: RawResponsesStreamEvent
Event 5: RawResponsesStreamEvent
Event 6: RawResponsesStreamEvent
Event 7: RawResponsesStreamEvent
Event 8: RawResponsesStreamEvent
Event 9: RawResponsesStreamEvent
Event 10: RunItemStreamEvent
Total events: 10
/Users/seratch/code/openai-agents-python/.venv/lib/python3.12/site-packages/pydantic/main.py:464: UserWarning: Pydantic serializer warnings:
  PydanticSerializationUnexpectedValue(Expected 10 fields but got 5: Expected `Message` - serialized value may not be as expected [field_name='message', input_value=Message(content='Hello! H...er_specific_fields=None), input_type=Message])
  PydanticSerializationUnexpectedValue(Expected `StreamingChoices` - serialized value may not be as expected [field_name='choices', input_value=Choices(finish_reason='st...r_specific_fields=None)), input_type=Choices])
  return self.__pydantic_serializer__.to_python(

after with the opt-in env variable:

$ OPENAI_AGENTS_ENABLE_LITELLM_SERIALIZER_PATCH=1 uv run python test.py
Event 1: AgentUpdatedStreamEvent
Event 2: RawResponsesStreamEvent
Event 3: RawResponsesStreamEvent
Event 4: RawResponsesStreamEvent
Event 5: RawResponsesStreamEvent
Event 6: RawResponsesStreamEvent
Event 7: RawResponsesStreamEvent
Event 8: RawResponsesStreamEvent
Event 9: RawResponsesStreamEvent
Event 10: RunItemStreamEvent
Total events: 10

@seratch seratch added this to the 0.6.x milestone Jan 12, 2026
@seratch seratch merged commit 9cc8958 into main Jan 12, 2026
9 checks passed
@seratch seratch deleted the issue-2291-litellm-warning branch January 12, 2026 01:26
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.

Pydantic 2.12+ Serialization Warnings with Claude Models in openai-agents 0.6.5

2 participants