Skip to content

ChatCompletion streaming with AzureOpenAI provider #2297

@ciresimo

Description

@ciresimo

Please read this first

  • Have you read the docs?Agents SDK docs Yes
  • Have you searched for related issues? Others may have had similar requests Yes

Question

I need to integrate an agent in a third-party front-end that accepts only ChatCompletions messages (the platform was firstly built to use a "plain" OpenAI call)
I'm using a AsyncAzureOpenAI client (but I've tried using the standard AsyncOpenAI following the indications reported here https://learn.microsoft.com/en-us/azure/ai-foundry/openai/api-version-lifecycle?view=foundry-classic&tabs=python) which by default streams using Response API
I've tried changing the behavior by using set_default_openai_api("chat_completions") as suggested in the documentation. I've also tried with OpenAIProvider by setting use_responses=True and adding it to the runner
Runner.run_streamed(starting_agent=agent, input=prompt, run_config=RunConfig(model_provider=PROVIDER)). In none of the cases anything changes and I still get ResponseStreamEvents like this

RawResponsesStreamEvent(data=ResponseTextDeltaEvent(content_index=0, delta=' per', item_id='fake_id', logprobs=[], output_index=0, sequence_number=270, type='response.output_text.delta'), type='raw_response_event')

I'm wondering if I'm miss some settings, there's something wrong with my setup or if this is a known limitations

set_default_openai_api seems to have an effect only for OpenAIProvider, is this correct? And how should it be used to achieve the correct result?

Not sure if I gave all relevant information. Let me know if more details from my side are required. I leve here a snippet of the code I've being using

import asyncio
from openai import AsyncAzureOpenAI
from openai import AsyncOpenAI

from agents import Agent, OpenAIChatCompletionsModel, set_default_openai_api, OpenAIProvider, Runner, RunConfig

async def test_api():
    
    set_default_openai_api("chat_completions")
    async_client = AsyncOpenAI(
        api_key=AZURE_OPENAI_API_KEY,
        base_url="https://<company>.openai.azure.com/openai/v1/"
    ) # Same with AsyncAzureOpenAI
    MODEL = OpenAIChatCompletionsModel(
    model="gpt-4.1",
    openai_client=async_client
    )
    # agent definition
    agent = Agent(
        name="Pippo",
        model=MODEL,
        instructions="You are my personal assistant"
    )

    PROVIDER = OpenAIProvider(openai_client=async_client, use_responses=True)

    result =  Runner.run_streamed(
        starting_agent=agent, input="Hello",  run_config=RunConfig(model_provider=PROVIDER)  )


    async for event in result.stream_events():
        print(event)


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

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionQuestion about using the SDK

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions