[Tongyi]KeyError: 'tool_calls' when using Tongyi model without tool calls #959

Closed
opened 2026-02-16 10:21:07 -05:00 by yindo · 1 comment
Owner

Originally created by @Nov1c444 on GitHub (Jan 30, 2026).

Originally assigned to: @Nov1c444 on GitHub.

Self Checks

  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues Dify issues & Dify Official Plugins, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

1.11.4

Plugin version

0.1.23

Cloud or Self Hosted

Self Hosted (Source)

Description

In models/tongyi/models/llm/llm.py, the code uses getattr() to access tool_calls from the response message, but message is a dict-like object, not a regular object. This causes a KeyError when the tool_calls key is not present in the response.

✔️ Error log

No response

Originally created by @Nov1c444 on GitHub (Jan 30, 2026). Originally assigned to: @Nov1c444 on GitHub. ### Self Checks - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [Dify issues](https://github.com/langgenius/dify/issues) & [Dify Official Plugins](https://github.com/langgenius/dify-official-plugins/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version 1.11.4 ### Plugin version 0.1.23 ### Cloud or Self Hosted Self Hosted (Source) ### Description In models/tongyi/models/llm/llm.py, the code uses getattr() to access tool_calls from the response message, but message is a dict-like object, not a regular object. This causes a KeyError when the tool_calls key is not present in the response. ### ✔️ Error log _No response_
yindo added the bug label 2026-02-16 10:21:07 -05:00
yindo closed this issue 2026-02-16 10:21:07 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jan 30, 2026):

This KeyError happens because the code in models/tongyi/models/llm/llm.py tries to access tool_calls using getattr() on a dict-like message object, which fails if tool_calls isn't present. The correct approach is to use message.get('tool_calls', []) or check if 'tool_calls' is in the message before accessing it. This bug has been fixed in recent updates—see dify-official-plugins PR #2513 (merged 2026-01-29) and Dify PR #30083. Upgrading to the latest versions will resolve the issue.

If you can't upgrade right away, you can patch the code by replacing getattr(response.output.choices[0].message, 'tool_calls', []) with response.output.choices[0].message.get('tool_calls', []) to safely handle missing keys and prevent the KeyError. For streaming responses, the code already checks for 'tool_calls' in the message dict, so the error only affects non-streaming paths.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Jan 30, 2026): <!-- Answer --> This KeyError happens because the code in models/tongyi/models/llm/llm.py tries to access tool_calls using getattr() on a dict-like message object, which fails if tool_calls isn't present. The correct approach is to use message.get('tool_calls', []) or check if 'tool_calls' is in the message before accessing it. This bug has been fixed in recent updates—see [dify-official-plugins PR #2513](https://github.com/langgenius/dify-official-plugins/pull/2513) (merged 2026-01-29) and [Dify PR #30083](https://github.com/langgenius/dify/pull/30083). Upgrading to the latest versions will resolve the issue. If you can't upgrade right away, you can patch the code by replacing getattr(response.output.choices[0].message, 'tool_calls', []) with response.output.choices[0].message.get('tool_calls', []) to safely handle missing keys and prevent the KeyError. For streaming responses, the code already checks for 'tool_calls' in the message dict, so the error only affects non-streaming paths. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/5e0d3e90-f999-4d1d-bfb9-86f9419ef9f7?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/5e0d3e90-f999-4d1d-bfb9-86f9419ef9f7?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/5e0d3e90-f999-4d1d-bfb9-86f9419ef9f7?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/5e0d3e90-f999-4d1d-bfb9-86f9419ef9f7?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/5e0d3e90-f999-4d1d-bfb9-86f9419ef9f7?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/5e0d3e90-f999-4d1d-bfb9-86f9419ef9f7?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/5e0d3e90-f999-4d1d-bfb9-86f9419ef9f7?feedback_type=other)</sup> [![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=This%20KeyError%20happens%20because%20the%20code%20in%20models/tongyi/models/llm/llm.py%20tries%20to%20access%20tool_calls%20using%20getattr%28%29%20on%20a%20dict-like%20message%20object%2C%20which%20fails%20if%20tool_calls%20isn%27t%20present.%20The%20correct%20approach%20is%20to%20use%20message.get%28%27tool_calls%27%2C%20%5B%5D%29%20or%20check%20if%20%27tool_calls%27%20is%20in%20the%20message%20before%20accessing%20it.%20This%20bug%20has%20been%20fixed%20in%20recent%20updates%E2%80%94see%20%5Bdify-official-plugins%20PR%20%232513%5D%28https%3A//github.com/langgenius/dify-official-plugins/pull/2513%29%20%28merged%202026-01-29%29%20and%20%5BDify%20PR%20%2330083%5D%28https%3A//github.com/langgenius/dify/pull/30083%29.%20Upgrading%20to%20the%20latest%20versions%20will%20resolve%20the%20issue.%0A%0AIf%20you%20can%27t%20upgrade%20right%20away%2C%20you%20can%20patch%20the%20code%20by%20replacing%20getattr%28response.output.choices%5B0%5D.message%2C%20%27tool_calls%27%2C%20%5B%5D%29%20with%20response.output.choices%5B0%5D.message.get%28%27tool_calls%27%2C%20%5B%5D%29%20to%20safely%20handle%20missing%20keys%20and%20prevent%20the%20KeyError.%20For%20streaming%20responses%2C%20the%20code%20already%20checks%20for%20%27tool_calls%27%20in%20the%20message%20dict%2C%20so%20the%20error%20only%20affects%20non-streaming%20paths.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify-official-plugins/issues/2521)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-official-plugins#959