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-OpenaiMemoryVector StoresChainsAffected 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

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state