Skip to content

Support reasoning from LM Studio OpenAI Responses API with gpt-oss #3191

@umut-firat

Description

@umut-firat

Initial Checks

Description

I utilizing LM Studio with openai/gpt-oss-20b model to develop my applications. Before LM Studio got OpenAI Response API support, I was usingOpenAIChatModel which was just working fine. After the update and switching to it with new Pydantic AI v1.1.0, I'm getting following warning and ThinkingPart stopped working.

warnings.warn(
/Users/umutfirat/myproject/.venv/lib/python3.13/site-packages/pydantic_ai/models/openai.py:1904: UserWarning: Handling of this event type is not yet implemented. Please report on our GitHub: ResponseReasoningTextDeltaEvent(content_index=0, delta=' bur', item_id='rs_w2j8k5cvclmzxum5sgkhr', output_index=0, sequence_number=34, type='response.reasoning_text.delta')

I'm also sharing my function to understand what I'm doing wrong. My theory is maybe pydantic_ai.models.openai.OpenAIResponsesStreamedResponse missing Responses API parts? Thanks in advance.

Example Code

import json
from fastapi.responses import StreamingResponse
from pydantic_ai import (
    Agent,
    FinalResultEvent,
    PartDeltaEvent,
    PartStartEvent,
    TextPartDelta,
    ThinkingPartDelta,
)
from pydantic_ai.messages import TextPart, ThinkingPart
from pydantic_ai.models.openai import OpenAIResponsesModel, OpenAIResponsesModelSettings
from pydantic_ai.providers.openai import OpenAIProvider


async def create_ai_response(id: str, text: str):
    """
    Create an AI response using an agent with streaming output.
    """
    agent = Agent(
        model=OpenAIResponsesModel(
            "openai/gpt-oss-20b",
            provider=OpenAIProvider(base_url="http://localhost:1234/v1"),
        ),
        model_settings=OpenAIResponsesModelSettings(
            openai_reasoning_effort="medium", openai_reasoning_summary="detailed"
        ),
    )

    async def create_stream_output():
        """
        Stream the agent's response as Server-Sent Events (SSE).
        """
        async with agent.iter(text) as agent_run:
            async for node in agent_run:
                if Agent.is_user_prompt_node(node):
                    print(f"=== UserPromptNode: {node.user_prompt} ===")
                elif Agent.is_model_request_node(node):
                    async with node.stream(agent_run.ctx) as stream:
                        async for event in stream:
                            print(event)
                            if isinstance(event, PartStartEvent):
                                if isinstance(event.part, ThinkingPart):
                                    yield f"data: {json.dumps({'type': 'reasoning-start', 'id': id})}\n\n"
                                elif isinstance(event.part, TextPart):
                                    yield f"data: {json.dumps({'type': 'text-start', 'id': id})}\n\n"
                            elif isinstance(event, PartDeltaEvent):
                                if isinstance(event.delta, ThinkingPartDelta):
                                    yield f"data: {json.dumps({'type': 'reasoning-delta', 'id': id, 'delta': event.delta.content_delta})}\n\n"
                                elif isinstance(event.delta, TextPartDelta):
                                    yield f"data: {json.dumps({'type': 'text-delta', 'id': id, 'delta': event.delta.content_delta})}\n\n"
                            elif isinstance(event, FinalResultEvent):
                                print(
                                    "[Result] The model started producing a final result"
                                )

                elif Agent.is_call_tools_node(node):
                    ...
                elif Agent.is_end_node(node):
                    print(f"=== Final Agent Output: {agent_run.result.output} ===")
                    yield "data: [DONE]\n\n"

    return StreamingResponse(create_stream_output(), media_type="text/event-stream")

Python, Pydantic AI & LLM client version

My `pyproject.toml`:

.toml
[project]
name = "myproject"
version = "0.1.0"
description = "My Project"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
    "fastapi[all]>=0.119.0",
    "langchain-text-splitters>=0.3.11",
    "lmstudio>=1.5.0",
    "psycopg[binary]>=3.2.10",
    "pydantic-ai>=1.1.0",
    "pypdf>=6.1.1",
    "qdrant-client>=1.15.1",
    "sqlalchemy>=2.0.44",
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions