ChatPerplexity(use_responses_api=True).invoke() raises pydantic ValidationError on every call

这个报错发生在 langchain-perplexity 以 use_responses_api=True 调用 Perplexity 时,请求负载中的消息缺少 type 字段,导致 SDK 的 pydantic 模型无法通过校验。优先检查并修复消息转换逻辑中缺失的 type: "message"

快速结论:这个报错发生在 langchain-perplexity 以 use_responses_api=True 调用 Perplexity 时,请求负载中的消息缺少 type 字段,导致 SDK 的 pydantic 模型无法通过校验。优先检查并修复消息转换逻辑中缺失的 type: "message" 字段。

适用环境:langchain-perplexity 集成包,Perplexity Responses API(use_responses_api=True),Python 环境,LangChain 相关依赖。

最快修复方案:暂无确认的一步修复方案;Issue 中给出的修复需要修改 langchain_perplexity/chat_models.py 中的 _translate_responses_input 函数,为消息字典补上 type 字段。

注意事项:该修复已合并到 master 并验证通过,但尚未确认是否已随 langchain-perplexity 正式版本发布;升级依赖前应确认版本号。

问题场景

用户在 langchain-perplexity 中使用 ChatPerplexity 时,设置 use_responses_api=True,调用 llm.invoke("hi") 触发报错。问题是发出的请求负载中,消息对象缺失 type 字段,导致 pydantic 的 discriminated union 无法识别消息类型。

报错原文

pydantic_core._pydantic_core.ValidationError: 9 validation errors for ResponsesRequestInput
input.str
  Input should be a valid string [type=string_type, input_value=[{'role': 'user', 'content': 'hi'}], input_type=list]
input.list[union[InputMessageInput,FunctionCallOutputInputInput,FunctionCallInputInput]].0.InputMessageInput.type
  Field required [type=missing, input_value={'role': 'user', 'content': 'hi'}, input_type=dict]
input.list[union[InputMessageInput,FunctionCallOutputInputInput,FunctionCallInputInput]].0.FunctionCallOutputInputInput.call_id
  Field required [type=missing, input_value={'role': 'user', 'content': 'hi'}, input_type=dict]
input.list[union[InputMessageInput,FunctionCallOutputInputInput,FunctionCallInputInput]].0.FunctionCallOutputInputInput.output
  Field required [type=missing, input_value={'role': 'user', 'content': 'hi'}, input_type=dict]
input.list[union[InputMessageInput,FunctionCallOutputInputInput,FunctionCallInputInput]].0.FunctionCallOutputInputInput.type
  Field required [type=missing, input_value={'role': 'user', 'content': 'hi'}, input_type=dict]
input.list[union[InputMessageInput,FunctionCallOutputInputInput,FunctionCallInputInput]].0.FunctionCallInputInput.arguments
  Field required [type=missing, input_value={'role': 'user', 'content': 'hi'}, input_type=dict]
input.list[union[InputMessageInput,FunctionCallOutputInputInput,FunctionCallInputInput]].0.FunctionCallInputInput.call_id
  Field required [type=missing, input_value={'role': 'user', 'content': 'hi'}, input_type=dict]
input.list[union[InputMessageInput,FunctionCallOutputInputInput,FunctionCallInputInput]].0.FunctionCallInputInput.name
  Field required [type=missing, input_value={'role': 'user', 'content': 'hi'}, input_type=dict]
input.list[union[InputMessageInput,FunctionCallOutputInputInput,FunctionCallInputInput]].0.FunctionCallInputInput.type

原因分析

可能原因:_translate_responses_input 函数在构建请求负载时,有两处消息字典没有携带 type 字段。Perplexity SDK 的 ResponsesRequestInput.input 是一个 discriminated union,pydantic 必须根据 type 字段才能区分是 InputMessageInputFunctionCallOutputInputInput 还是 FunctionCallInputInput。缺了该字段,整个请求就会被 9 个校验错误拒绝。具体位置是:

  • role == "assistant" 且存在 tool_calls 的分支中,assistant 文本消息被加入时缺少 type: "message"
  • 在转换普通角色消息(system、user、assistant、developer)的 else 分支中,直接透传了原始消息字典,没有补上 type: "message"

环境排查

  • 确认 langchain-perplexity 版本是否包含 #39774 的修复(该修复已合入 master)。
  • 确认 Python 环境中 pydantic 版本与 Perplexity SDK 兼容。
  • 确认是否在 2026-09-27 Sonar Chat Completions 下线前必须迁移到 Responses API(Issue 中提到的迁移阻塞场景)。

解决步骤

  1. 升级 langchain-perplexity 到包含 #39774 修复的版本(该修复已合并到 master,后续版本会包含)。
  2. 如果无法升级,可优先尝试在本地源码中修改 langchain_perplexity/chat_models.py_translate_responses_input 函数:在 role == "assistant" 且存在 tool_calls 的分支中,将 assistant 文本消息改为 {"type": "message", "role": "assistant", "content": text}
  3. role in {"assistant", "system", "user", "developer"}else 分支中,改为 translated.append({**message, "type": "message"})
  4. 修改后补充或更新单元测试,断言请求负载中的消息对象包含 type: "message"
  5. 执行 ChatPerplexity(use_responses_api=True).invoke("hi") 验证请求可通过 pydantic 校验。

验证方法

修复后,调用 ChatPerplexity(model="openai/gpt-5.6-sol", api_key=os.environ["PERPLEXITY_API_KEY"], use_responses_api=True).invoke("hi"),确认不再抛出 pydantic ValidationError,且能正常返回 AIMessage。Issue 中已对单轮和多轮调用分别做了验证。

参考来源

langchain-ai/langchain #39775

GamsGo AI

AI 工具推荐

想把多个 AI 模型放在一个入口?

GamsGo AI 集成 ChatGPT、DeepSeek、Gemini、Claude、Midjourney、Veo 等常用模型,适合写作、绘图、视频和日常 AI 工作流。

了解 GamsGo AI

推广链接:通过此链接购买,我可能获得佣金,不影响你的价格。

这个方案解决了吗?

celebrityanime
celebrityanime
文章: 19676

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注