mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
Python: fix(openai): guard against null delta in streaming chunks from non-co… (#5734)
* fix(openai): guard against null delta in streaming chunks from non-compliant providers (#5732) * chore: resolve nit and align with project style --------- Co-authored-by: Sergey Borisov <sergey.borisov@dataimpact.io> Co-authored-by: Giles Odigwe <79032838+giles17@users.noreply.github.com>
This commit is contained in:
@@ -728,10 +728,16 @@ class RawOpenAIChatCompletionClient( # type: ignore[misc]
|
||||
|
||||
for choice in chunk.choices:
|
||||
chunk_metadata.update(self._get_metadata_from_chat_choice(choice))
|
||||
contents.extend(self._parse_tool_calls_from_openai(choice))
|
||||
if choice.finish_reason:
|
||||
finish_reason = choice.finish_reason # type: ignore[assignment]
|
||||
|
||||
# Some OpenAI-compatible providers (e.g. Azure) send `"delta": null`
|
||||
# on finish chunks instead of the spec-compliant `"delta": {}`.
|
||||
# Guard here so all content-parsing below can assume delta is present.
|
||||
if choice.delta is None: # pyright: ignore[reportUnnecessaryComparison]
|
||||
continue
|
||||
|
||||
contents.extend(self._parse_tool_calls_from_openai(choice))
|
||||
if text_content := self._parse_text_from_openai(choice):
|
||||
contents.append(text_content)
|
||||
if reasoning_details := getattr(choice.delta, "reasoning_details", None):
|
||||
|
||||
Reference in New Issue
Block a user