Skip to content

Python: [Bug]: OpenAIChatCompletionClient exception #5732

@unbuilt

Description

@unbuilt

Description

While build hosted agent with Agent Framework,
When use OpenAIChatCompletionClient, to call gpt-4.1 model, the client may raise exception like:

14:18:44.955 [stream] stderr 2026-05-08 06:18:44,954 ERROR azure.ai.agentserver: Handler raised after response.created (response_id=caresp_fff952c7a059d512006yfPdEk7uoALbythPrQXStTjx2tQ2HxF)
14:18:44.955 [stream] stderr Traceback (most recent call last):
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework_openai/_chat_completion_client.py", line 525, in _stream
14:18:44.955 [stream] stderr yield self._parse_response_update_from_openai(chunk)
14:18:44.955 [stream] stderr ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework_openai/_chat_completion_client.py", line 735, in _parse_response_update_from_openai
14:18:44.955 [stream] stderr if text_content := self._parse_text_from_openai(choice):
14:18:44.955 [stream] stderr ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework_openai/_chat_completion_client.py", line 776, in _parse_text_from_openai
14:18:44.955 [stream] stderr if message.content:
14:18:44.955 [stream] stderr ^^^^^^^^^^^^^^^
14:18:44.955 [stream] stderr AttributeError: 'NoneType' object has no attribute 'content'
14:18:44.955 [stream] stderr
14:18:44.955 [stream] stderr The above exception was the direct cause of the following exception:
14:18:44.955 [stream] stderr
14:18:44.955 [stream] stderr Traceback (most recent call last):
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/azure/ai/agentserver/responses/hosting/_orchestrator.py", line 1243, in _process_handler_events
14:18:44.955 [stream] stderr async for raw in _iter_with_winddown(handler_iterator, ctx.cancellation_signal):
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/azure/ai/agentserver/responses/hosting/_orchestrator.py", line 156, in _iter_with_winddown
14:18:44.955 [stream] stderr item = await aiter.anext()
14:18:44.955 [stream] stderr ^^^^^^^^^^^^^^^^^^^^^^^
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/azure/ai/agentserver/responses/hosting/_routing.py", line 347, in _await_and_normalize
14:18:44.955 [stream] stderr async for event in self._normalize_handler_result(inner):
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework_foundry_hosting/_responses.py", line 341, in _handle_inner_agent
14:18:44.955 [stream] stderr async for update in self._agent.run(stream=True, **run_kwargs): # type: ignore[reportUnknownMemberType]
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework/_types.py", line 3024, in anext
14:18:44.955 [stream] stderr update: UpdateT = await self._iterator.anext()
14:18:44.955 [stream] stderr ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework/_types.py", line 3024, in anext
14:18:44.955 [stream] stderr update: UpdateT = await self._iterator.anext()
14:18:44.955 [stream] stderr ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework/_types.py", line 3024, in anext
14:18:44.955 [stream] stderr update: UpdateT = await self._iterator.anext()
14:18:44.955 [stream] stderr ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework/_tools.py", line 2559, in _stream
14:18:44.955 [stream] stderr async for update in inner_stream:
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework/_types.py", line 3024, in anext
14:18:44.955 [stream] stderr update: UpdateT = await self._iterator.anext()
14:18:44.955 [stream] stderr ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14:18:44.955 [stream] stderr File "/usr/local/lib/python3.12/site-packages/agent_framework_openai/_chat_completion_client.py", line 540, in _stream
14:18:44.955 [stream] stderr raise ChatClientException(
14:18:44.955 [stream] stderr agent_framework.exceptions.ChatClientException: ("<class 'agent_framework_openai._chat_completion_client.OpenAIChatCompletionClient'> service failed to complete the prompt: 'NoneType' object has no attribute 'content'", AttributeError("'NoneType' object has no attribute 'content'"))

Code Sample

    """
    client = FoundryChatClient(
        project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
        model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
        credential=DefaultAzureCredential(),
    )
    """

    project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"]
    openai_url = project_endpoint.rstrip("/") + "/openai/v1"

    chat_client = OpenAIChatCompletionClient(
        base_url=openai_url,
        model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
        api_key=_token_provider,
    )

    agent = Agent(
        client=chat_client,
        instructions="You are a friendly assistant. Keep your answers brief.",
        # History will be managed by the hosting infrastructure, thus there
        # is no need to store history by the service. Learn more at:
        # https://developers.openai.com/api/reference/resources/responses/methods/create
        default_options={"store": False},
    )

Error Messages / Stack Traces

Package Versions

agent-framework-1.3.0

Python Version

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions