
Reasoning trace is not being returned when using an AzureChatOpenAI model from Microsoft Foundry
快速结论:使用 AzureChatOpenAI 流式调用时,推理痕迹(reasoning trace)不会出现在 content_blocks 中,而是位于每个 AIMessageChunk 的 additional_kwargs["reasoning"] 下。优先检查是否从 additional_kwargs 提取,而不是 content_blocks。
问题场景
用户在 LangChain 中使用 AzureChatOpenAI 模型(部署于 Microsoft Foundry 的 gpt-5-nano),设置 reasoning={"effort": "medium", "summary": "detailed"} 并通过 model.stream() 流式输出,期望从 chunk.content_blocks 获取推理层 token,但输出为空。该模型在直接使用 OpenAI Responses API 时可以正常返回推理痕迹。
报错原文
No reasoning trace is output.
Expected output: reasoning tokens should be output
原因分析
这不是一个功能 Bug。推理摘要(reasoning summary)确实通过 SDK 流式返回,但 LangChain 的 AzureChatOpenAI 集成将其放在 AIMessageChunk 的 additional_kwargs 中,而非 content_blocks。用户按照文档示例从 chunk.content_blocks 提取,自然拿不到数据。文档(docs)示例代码未覆盖 AzureChatOpenAI 的实际情况。
环境排查
- langchain-openai: 0.3.35
- langchain-core: 1.2.0 (需确认)
- langchain: 1.1.3
- openai: 2.8.1
- Python: 3.12.12
- OS: macOS Darwin (仅报告用)
解决步骤
- 将提取逻辑从
content_blocks改为additional_kwargs。示例代码如下:for chunk in model.stream("what is capital of France?"): additional_kwargs = getattr(chunk, "additional_kwargs", None) or {} reasoning = additional_kwargs.get("reasoning") if isinstance(reasoning, dict): summary = reasoning.get("summary") if isinstance(summary, list) and summary: for item in summary: if isinstance(item, dict) and item.get("type") == "summary_text": text = item.get("text") if text: print(text, end="") - 如果需要同时处理
content和推理文本,可在同一循环中分别提取:for chunk in model.stream(...): if chunk.content: print(chunk.content, end="") # 同时从 additional_kwargs 提取推理 ...
验证方法
运行修改后的代码,确认控制台能够打印出推理摘要文本(例如 “The capital of France is Paris…” 等推理过程)。如果输出正常,说明问题解决。



