news 2026/6/10 20:52:56

Python LangChain 开发问题:TypeError: “Could not resolve authentication method. Expected either api_key

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python LangChain 开发问题:TypeError: “Could not resolve authentication method. Expected either api_key
fromlangchain.agentsimportcreate_agentdefget_weather(city:str)->str:"""获取指定城市的天气"""returnf"{city}天气总是晴朗!"agent=create_agent(model="anthropic:claude-sonnet-4-5",tools=[get_weather],system_prompt="你是一个乐于助人的助手",)# 执行代理agent.invoke({"messages":[{"role":"user","content":"旧金山天气如何?"}]})
  • 在 Python 开发中,使用 LangChain 时,执行上述代码时,出现如下错误信息
D:\PythonCode\LangChainTest\.venv\Scripts\python.exe D:\PythonCode\LangChainTest\main.py D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain_core\_api\deprecation.py:26: UserWarning: Core Pydantic V1 functionality isn't compatible with Python 3.14 or greater. from pydantic.v1.fields import FieldInfo as FieldInfoV1 Traceback (most recent call last): File "D:\PythonCode\LangChainTest\main.py", line 14, in <module> agent.invoke( ~~~~~~~~~~~~^ {"messages": [{"role": "user", "content": "旧金山天气如何?"}]} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langgraph\pregel\main.py", line 3068, in invoke for chunk in self.stream( ~~~~~~~~~~~^ input, ^^^^^^ ...<10 lines>... **kwargs, ^^^^^^^^^ ): ^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langgraph\pregel\main.py", line 2643, in stream for _ in runner.tick( ~~~~~~~~~~~^ [t for t in loop.tasks.values() if not t.writes], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<2 lines>... schedule_task=loop.accept_push, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ): ^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langgraph\pregel\_runner.py", line 167, in tick run_with_retry( ~~~~~~~~~~~~~~^ t, ^^ ...<10 lines>... }, ^^ ) ^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langgraph\pregel\_retry.py", line 42, in run_with_retry return task.proc.invoke(task.input, config) ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langgraph\_internal\_runnable.py", line 656, in invoke input = context.run(step.invoke, input, config, **kwargs) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langgraph\_internal\_runnable.py", line 400, in invoke ret = self.func(*args, **kwargs) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain\agents\factory.py", line 1131, in model_node response = _execute_model_sync(request) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain\agents\factory.py", line 1102, in _execute_model_sync output = model_.invoke(messages) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain_core\runnables\base.py", line 5548, in invoke return self.bound.invoke( ~~~~~~~~~~~~~~~~~^ input, ^^^^^^ self._merge_configs(config), ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ **{**self.kwargs, **kwargs}, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain_core\language_models\chat_models.py", line 398, in invoke self.generate_prompt( ~~~~~~~~~~~~~~~~~~~~^ [self._convert_input(input)], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...<6 lines>... **kwargs, ^^^^^^^^^ ).generations[0][0], ^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1117, in generate_prompt return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain_core\language_models\chat_models.py", line 927, in generate self._generate_with_cache( ~~~~~~~~~~~~~~~~~~~~~~~~~^ m, ^^ ...<2 lines>... **kwargs, ^^^^^^^^^ ) ^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain_core\language_models\chat_models.py", line 1221, in _generate_with_cache result = self._generate( messages, stop=stop, run_manager=run_manager, **kwargs ) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain_anthropic\chat_models.py", line 1915, in _generate data = self._create(payload) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\langchain_anthropic\chat_models.py", line 1777, in _create return self._client.messages.create(**payload) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\anthropic\_utils\_utils.py", line 282, in wrapper return func(*args, **kwargs) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\anthropic\resources\messages\messages.py", line 930, in create return self._post( ~~~~~~~~~~^ "/v1/messages", ^^^^^^^^^^^^^^^ ...<26 lines>... stream_cls=Stream[RawMessageStreamEvent], ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ) ^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\anthropic\_base_client.py", line 1326, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\anthropic\_base_client.py", line 1035, in request request = self._build_request(options, retries_taken=retries_taken) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\anthropic\_base_client.py", line 506, in _build_request headers = self._build_headers(options, retries_taken=retries_taken) File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\anthropic\_base_client.py", line 447, in _build_headers self._validate_headers(headers_dict, custom_headers) ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\PythonCode\LangChainTest\.venv\Lib\site-packages\anthropic\_client.py", line 200, in _validate_headers raise TypeError( '"Could not resolve authentication method. Expected either api_key or auth_token to be set. Or for one of the `X-Api-Key` or `Authorization` headers to be explicitly omitted"' ) TypeError: "Could not resolve authentication method. Expected either api_key or auth_token to be set. Or for one of the `X-Api-Key` or `Authorization` headers to be explicitly omitted" During task with name 'model' and id '9c34258b-c90f-d21b-f23b-504e5fe2b3f1' Process finished with exit code 1
问题原因
  • 需要设置一个 Claude(Anthropic)账户并获取 API 密钥
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/10 10:51:17

异常行为检测_Cascade_Mask_RCNN_r50_rsb_pre_FPN_1x_Coco训练

1. &#x1f31f; 超全YOLO系列模型大盘点 &#x1f680; 嗨&#xff0c;各位CV小伙伴&#xff01;今天来给大家唠唠计算机视觉领域最火的YOLO系列模型~ 从YOLOv1到最新的YOLOv13&#xff0c;还有各种魔改变种&#xff0c;简直让人眼花缭乱&#xff01;&#x1f631; 这篇文章…

作者头像 李华
网站建设 2026/6/10 10:51:30

YOLOv8改进实战:基于StarNet的水果品质检测与分级系统(附完整代码)

1. YOLOv8改进实战&#xff1a;基于StarNet的水果品质检测与分级系统&#xff08;附完整代码&#xff09; 1.1. 摘要 本文介绍了一个基于改进YOLOv8的水果品质检测与分级系统&#xff0c;通过引入StarNet注意力机制显著提升了模型对小尺寸水果缺陷的检测能力。系统实现了从图…

作者头像 李华
网站建设 2026/6/10 12:21:20

Adobe Source Sans 3 开源字体:从入门到精通的5个实用技巧

Adobe Source Sans 3 开源字体&#xff1a;从入门到精通的5个实用技巧 【免费下载链接】source-sans Sans serif font family for user interface environments 项目地址: https://gitcode.com/gh_mirrors/so/source-sans Adobe Source Sans 3是一款专为现代数字界面设计…

作者头像 李华
网站建设 2026/6/10 4:29:07

QEMU交互式运行时分析器QIRA完整使用指南

QEMU交互式运行时分析器QIRA完整使用指南 【免费下载链接】qira QEMU Interactive Runtime Analyser 项目地址: https://gitcode.com/gh_mirrors/qi/qira 项目概述与核心价值 QIRA&#xff08;QEMU Interactive Runtime Analyser&#xff09;是一款基于QEMU构建的交互式…

作者头像 李华
网站建设 2026/6/10 9:18:24

Java try-with-resources写法

try (ZipOutputStream zos new ZipOutputStream(response.getOutputStream())) {// 使用资源 } catch (Exception e) {// 处理异常e.printStackTrace(); }该写法是Java 7引入的 “try-with-resources” 写法&#xff0c;也叫自动资源管理&#xff08;ARM, Automatic Resource …

作者头像 李华