agentscope icon indicating copy to clipboard operation
agentscope copied to clipboard

[Bug]:调用deepseek官网模型的时候报错

Open lokiwizard opened this issue 2 months ago • 7 comments

`api_key = OmegaConf.load(api_key_path).get("deepseek").get("api_key")

model = OpenAIChatModel( model_name="deepseek-chat", api_key=api_key, client_args={"base_url": "https://api.deepseek.com", }, **kwargs, )

formatter = OpenAIChatFormatter() memory = InMemoryMemory()

agent = ReActAgent( name=agent_name, sys_prompt=sys_prompt, model=model, formatter=formatter, memory=memory, ) ` 在第一轮对话的时候消息里会打印:Error in block input ['response'],然后模型会正常回复消息 在第二轮对话直接报错:openai.BadRequestError: Error code: 400 - {'error': {'message': 'Failed to deserialize the JSON body into the target type: messages[4]: invalid type: sequence, expected a string at line 1 column 1838', 'type': 'invalid_request_error', 'param': None, 'code': 'invalid_request_error'}}

lokiwizard avatar Oct 29 '25 07:10 lokiwizard

Please use DeepSeekChatFormatter

qbc2016 avatar Oct 29 '25 07:10 qbc2016

Please use DeepSeekChatFormatter

第二个问题确实解决了,第一个还是有问题,应该是传入工具参数的时候有问题,这是传入的工具参数,'tools': [{'type': 'function', 'function': {'name': 'generate_response', 'parameters': {'properties': {'response': {'description': 'Your response to the user.', 'type': 'string'}}, 'required': ['response'], 'type': 'object'},里面有个字段required好像和openai模型不兼容,我在OpenAIChatModel类下加了一行kwargs.pop("tools", None),就没有问题了,但是工具调用应该是用不了了

lokiwizard avatar Oct 29 '25 08:10 lokiwizard

Please use DeepSeekChatFormatter

第二个问题确实解决了,第一个还是有问题,应该是传入工具参数的时候有问题,这是传入的工具参数,'tools': [{'type': 'function', 'function': {'name': 'generate_response', 'parameters': {'properties': {'response': {'description': 'Your response to the user.', 'type': 'string'}}, 'required': ['response'], 'type': 'object'},里面有个字段required好像和openai模型不兼容,我在OpenAIChatModel类下加了一行kwargs.pop("tools", None),就没有问题了,但是工具调用应该是用不了了

应该不是工具格式的问题,因为这是function calling的标准格式,deepseek-chat模型是支持工具调用的,猜测是因为模型选择了generate_response,但是没有传入对应的response值导致的。能展示一下Error in block input ['response']的具体细节么

qbc2016 avatar Oct 29 '25 09:10 qbc2016

Please use DeepSeekChatFormatter

第二个问题确实解决了,第一个还是有问题,应该是传入工具参数的时候有问题,这是传入的工具参数,'tools': [{'type': 'function', 'function': {'name': 'generate_response', 'parameters': {'properties': {'response': {'description': 'Your response to the user.', 'type': 'string'}}, 'required': ['response'], 'type': 'object'},里面有个字段required好像和openai模型不兼容,我在OpenAIChatModel类下加了一行kwargs.pop("tools", None),就没有问题了,但是工具调用应该是用不了了

应该不是工具格式的问题,因为这是function calling的标准格式,deepseek-chat模型是支持工具调用的,猜测是因为模型选择了generate_response,但是没有传入对应的response值导致的。能展示一下Error in block input ['response']的具体细节么

Image,这个主要没有完整的报错信息,我看了而且还发现个很怪的bug, Image,发送“你好”时会返回一串Unicode。我尝试定位问题,发现应该是流式输出的问题,设置为stream=False就是ok的,

lokiwizard avatar Oct 29 '25 13:10 lokiwizard

我的环境:使用DeepSeekChatFormatter(),调用deepseek chat官方模型

遇到同样的问题,做了些调试,发现:

  • 是finish_function_pre_print_hook在将generate_response作为工具调用时的tool_use块替换为text内容造成的
  • 但问题的根因疑似是_json_loads_with_repair中的repair_json(json_str)方法尝试修复json字符串导致的(mangiucugna/json_repair

下面是完整调试过程:

我在异常触发处拿到了具体的exception错误,是因为generate_response的tool_use块的block["input"]格式是列表而非字典,导致get时异常:'list' object has no attribute 'get'

Image

而此时block["input"],也就是generate_response的tool_use块的传参长这样:

Image

显然我这边generate_response的tool_use参数数据是列表格式而非dict,第二个值是空字典.

~~不知道和这里的kwargs有无关系:~~ 同kwargs无关,已注释掉kwargs相关逻辑仍是列表格式

Image

于是我进一步对generate_response的参数做了些改动,以测试block["input"]的实际格式: 如下图,我给generate_response加了一个未使用的mode参数,并prompt其值固定

Image

此时仍能复现出同样的异常,且此时的block["input"]仍是list:

Image

因此我怀疑tool_use块是否在生成时就已经出现了问题,也即在_openai_model.py的_parse_openai_stream_response方法中,给_json_loads_with_repair的tool_call["input"]的原始json字符串有问题、或是_json_loads_with_repair本身的处理有问题。

Image

于是我将tool_call["input"]的值,以及_json_loads_with_repair的返回json,全部dump出来,发现了问题:

  • 首先,_json...repair返回的值,就已经是错误的list格式了: Image

  • 另外,tool_call["input"]的原始参数json字符串,并非list格式,而是response的value部分被截断的不完整内容

Image

因此推测,是否是_json_loads_with_repair方法在积极尝试修复tool调用参数的原始json字符串的过程中,会导致修复的结果变成list。于是我做了验证,dump了_json_loads_with_repairrepair_json(json_str)返回的修复后的json字符串:

Image Image

如图所示,果然是修复后变成了list格式的json字符串,基本定位到问题应该是出在_json_loads_with_repair中的repair_json(json_str)方法

btw,这个bug已经有人在json_repair项目中遇到过了:issue159 👍 哦才发现这个issue就是 @qbc2016 您这边提的😂😂😂,目前有建议的临时解决方案吗?

AmberSecurity avatar Nov 07 '25 06:11 AmberSecurity

我看官方已经提交修复了,可以尝试更新下json_repair的版本,我测了下失败的case,已经可以成功了。

qbc2016 avatar Nov 10 '25 06:11 qbc2016

Please use DeepSeekChatFormatter

第二个问题确实解决了,第一个还是有问题,应该是传入工具参数的时候有问题,这是传入的工具参数,'tools': [{'type': 'function', 'function': {'name': 'generate_response', 'parameters': {'properties': {'response': {'description': 'Your response to the user.', 'type': 'string'}}, 'required': ['response'], 'type': 'object'},里面有个字段required好像和openai模型不兼容,我在OpenAIChatModel类下加了一行kwargs.pop("tools", None),就没有问题了,但是工具调用应该是用不了了

应该不是工具格式的问题,因为这是function calling的标准格式,deepseek-chat模型是支持工具调用的,猜测是因为模型选择了generate_response,但是没有传入对应的response值导致的。能展示一下Error in block input ['response']的具体细节么

Image,这个主要没有完整的报错信息,我看了而且还发现个很怪的bug, Image,发送“你好”时会返回一串Unicode。我尝试定位问题,发现应该是流式输出的问题,设置为stream=False就是ok的,

这个有可能是generate_response的问题,我们最新的commit(#910)里对这个函数进行了重构,辛苦拉一下最新的代码再试一下是否还有问题

qbc2016 avatar Nov 11 '25 07:11 qbc2016

This issue is marked as stale because there has been no activity for 21 days. Remove stale label or add new comments or this issue will be closed in 3 day.

github-actions[bot] avatar Dec 02 '25 09:12 github-actions[bot]

Close this stale issue.

github-actions[bot] avatar Dec 06 '25 09:12 github-actions[bot]