AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

[Langchain-Openai] ChatOpenAI.Stream() Crashes On Null Function In Tool-Call Delta

When streaming a chat completion, a tool-call delta chunk that includes a null `function` field causes an `AttributeError` inside `_convert_delta_to_message_chunk`. The public `ChatOpenAI.stream()` API terminates unexpectedly instead of ignoring the empty field or preserving the partial tool call.

highConfidence 87%Langchain-OpenaiAffected V1.6.6

Origin Analysis

The `_convert_delta_to_message_chunk` function in `langchain_openai/chat_models/base.py` assumes that every tool-call delta contains a non-null `function` dictionary and directly calls `.get()` on it. The OpenAI Python SDK can deserialize the `function` field as `None`, leading to a `NoneType.get` AttributeError.
Use `httpx.MockTransport` to return a SSE chat completion chunk where `choices[0].delta.tool_calls[0].function` is `null`. Instantiate `ChatOpenAI` with `http_client=client` and call `list(model.stream('Call the tool'))`. The stream crashes with `AttributeError: 'NoneType' object has no attribute 'get'`.

Fixing Code Block

Edge Case Audit

The fix silently tolerates malformed or unexpected null function payloads. If downstream code assumes that a tool call chunk always has non-null name or args, it may still fail later. Add logging or metrics to track how often null function fields occur. Rollback recommendation: if a future OpenAI API update guarantees non-null function fields, the original code can be restored, but keeping the defensive guard is safer and has minimal overhead.

Ecosystem Topology