You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用LangChain时遇TypeError: Client对象无法JSON序列化问题求助

问题:LangChain调用OpenAI时出现TypeError: Object of type Client is not JSON serializable

问题场景

刚学习LangChain,执行一段简单代码时触发序列化错误,代码如下:

template = """Tell me a {adjective} joke about {subject}."""
prompt = PromptTemplate(template=template, input_variables=["adjective", "subject"])
llm_chain = LLMChain(prompt=prompt, llm=config.openaiExample, verbose=True)

llm_chain.predict(adjective="sad", subject="ducks")

格式化提示词生成后报错,提示内容:

Entering new LLMChain chain...
Prompt after formatting:
Tell me a sad joke about ducks.

完整报错栈:

Traceback (most recent call last):
  File "/Users/ianhui/code/bpe/bpe-python/test.py", line 11, in <module>
    llm_chain.predict(adjective="sad", subject="ducks")
  File "/opt/homebrew/lib/python3.11/site-packages/langchain/chains/llm.py", line 293, in predict
    return self(kwargs, callbacks=callbacks)[self.output_key]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/langchain/chains/base.py", line 312, in __call__
    raise e
  File "/opt/homebrew/lib/python3.11/site-packages/langchain/chains/base.py", line 306, in __call__
    self._call(inputs, run_manager=run_manager)
  File "/opt/homebrew/lib/python3.11/site-packages/langchain/chains/llm.py", line 103, in _call
    response = self.generate([inputs], run_manager=run_manager)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/langchain/chains/llm.py", line 115, in generate
    return self.llm.generate_prompt(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/langchain_core/language_models/llms.py", line 516, in generate_prompt
    return self.generate(prompt_strings, stop=stop, callbacks=callbacks, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/langchain_core/language_models/llms.py", line 666, in generate
    output = self._generate_helper(
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/langchain_core/language_models/llms.py", line 553, in _generate_helper
    raise e
  File "/opt/homebrew/lib/python3.11/site-packages/langchain_core/language_models/llms.py", line 540, in _generate_helper
    self._generate(
  File "/opt/homebrew/lib/python3.11/site-packages/langchain_community/llms/openai.py", line 1146, in _generate
    full_response = completion_with_retry(
                    ^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/langchain_community/llms/openai.py", line 122, in completion_with_retry
    return _completion_with_retry(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/tenacity/__init__.py", line 289, in wrapped_f
    return self(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/tenacity/__init__.py", line 379, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/tenacity/__init__.py", line 314, in iter
    return fut.result()
           ^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/opt/homebrew/lib/python3.11/site-packages/tenacity/__init__.py", line 382, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/langchain_community/llms/openai.py", line 120, in _completion_with_retry
    return llm.client.create(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/openai/api_resources/chat_completion.py", line 25, in create
    return super().create(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/openai/api_resources/abstract/engine_api_resource.py", line 153, in create
    response, _, api_key = requestor.request(
                           ^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/openai/api_requestor.py", line 216, in request
    result = self.request_raw(
             ^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/openai/api_requestor.py", line 509, in request_raw
    abs_url, headers, data = self._prepare_request_raw(
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/lib/python3.11/site-packages/openai/api_requestor.py", line 481, in _prepare_request_raw
    data = json.dumps(params).encode()
           ^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 258, in iterencode
    return _iterencode(o, 0)
           ^^^^^^^^^^^^^^^^^
  File "/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Client is not JSON serializable

使用环境版本:

  • Python 3.11.5
  • langchain 0.0.352
  • langchain-community 0.0.3
  • langchain-core 0.1.1
  • langsmith 0.0.71

疑问:

  • 是否有其他人遇到过此问题?
  • 可能的解决方案是什么?
  • 该问题是否与版本相关?

解决方案

问题根因

错误本质是传入LLMChain的config.openaiExample不是LangChain封装的OpenAI实例,而是直接创建的OpenAI原生Client对象。LangChain在调用API时会将LLM实例的参数打包序列化,原生Client对象无法被JSON序列化,因此触发报错。

具体解决步骤

  1. 改用LangChain封装的OpenAI类实例
    不要直接传入OpenAI原生Client,使用LangChain提供的OpenAI或ChatOpenAI类创建实例:

    from langchain_community.llms import OpenAI
    
    # 初始化LangChain封装的OpenAI实例
    llm = OpenAI(
        api_key="你的OpenAI API密钥",
        model_name="gpt-3.5-turbo-instruct"  # 根据需求选择模型
    )
    
    # 替换原代码中的config.openaiExample
    llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True)
    llm_chain.predict(adjective="sad", subject="ducks")
    
  2. 升级依赖版本
    当前使用的langchain-community(0.0.3)属于早期版本,与langchain-core(0.1.1)存在版本兼容性问题,建议升级到匹配的稳定版本:

    pip install --upgrade langchain langchain-community langchain-core
    

    升级后LangChain对OpenAI客户端的逻辑会更稳定,减少参数混入导致的序列化问题。

  3. 检查自定义config逻辑
    排查config.openaiExample的创建代码,确保没有错误地将原生Client赋值给该变量,应返回LangChain封装的LLM实例。

相关社区情况

这类问题在LangChain早期版本中较为常见,多是用户混淆了OpenAI原生SDK和LangChain封装类的用法,社区讨论中多次出现类似案例。


内容的提问来源于stack exchange,提问作者ianhui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 21:37:04