当前位置: 首页 > news >正文

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 密钥
http://www.cnnetsun.cn/news/22038.html

相关文章:

  • 西南民族大学软件工程25级研究生赴华清远见成都中心开启元宇宙实训之旅
  • Obsidian图像工具包:终极图片管理与编辑指南
  • 自主高性价比、高精度车规级姿态感知、倾角感知模组-应用消费级无人机、自动驾驶、机器人、智能制造、基础设施、智能穿戴等
  • ComfyUI智能修复技术:图像处理的革命性突破
  • 碳硅协同:人工智能作为碳基生命合作伙伴的终极形态分析
  • 小公司效率低、管理乱?一张《四维照妖镜》,照出你的“效率黑洞”
  • 拒绝无意义刷屏,打造高效率热点追踪,极空间部署『TrendRadar』
  • 3步精通JSON对比工具:从新手到高手的实战指南
  • lombok的几个核心注解是什么?
  • Qwen3-VL-30B-A3B-Thinking-FP8多模态大模型实战指南:从技术突破到产业落地
  • 庄散资金主买卖差、散买卖差
  • AI办公工具选型指南:从文档到PPT,这些工具如何提升效率?
  • Web 漏洞扫描入门没头绪?2025 十大工具(详细拆解),零基础也能从入门到精通!
  • Morisawa BIZ UDGothic 终极字体配置指南:提升文档专业度的免费利器
  • Markn:轻量级Markdown查看器的终极指南——提升文档阅读体验
  • 小白必看!大模型入门指南
  • 一篇图文彻底搞懂什么是AI Agent
  • Kubernetes备份工具API实战指南:从入门到精通
  • 18、Linux数据搜索、提取与归档全解析
  • 19、Linux 文件操作与编辑全解析
  • 日薪2000+的 “ 护网行动 ” 到底是什么?
  • 百度网盘秒传技术终极指南:零基础掌握极速文件传输
  • 2026年金融/咨询行业求职风向标:顶级简历模板权威榜单
  • 5大关键技巧彻底掌握AgentWeb:从基础配置到企业级实战
  • 轻松搞定视频下载:Seal让你的手机变身多媒体收藏库
  • FluidNC:ESP32运动控制的革命性解决方案
  • dify 导入工作流,会有些插件报错
  • 基于百度地图打造“美食地图”与3D路线规划
  • 【硬件新人指南】从零入门硬件行业:技能树、学习路径与职业规划
  • 《无人驾驶航空器飞行管理暂行条例》核心内容梳理