fix(anthropic): fix Anthropic tool call message ordering for Dify compatibility#3275
Conversation
…patibility The Anthropic API requires that assistant text content appears before tool_use blocks in the message history, especially when integrating with Dify which stores assistant text and tool calls separately. Previously, text content was appended after tool calls, violating this requirement and potentially causing errors. This commit refactors the message creation logic to ensure assistant prose is always placed before tool_use content. It introduces a helper method `_create_assistant_text_contents` to handle both string and structured text content, ensuring correct ordering according to Anthropic's tool-use guidelines.
There was a problem hiding this comment.
Code Review
This pull request updates the Anthropic LLM implementation to ensure assistant text prose is placed before tool calls, preventing any text from landing between tool_use and tool_result turns. It also introduces a helper method _create_assistant_text_contents to handle both string and list-based message contents. The review feedback suggests using isinstance instead of checking content.type and casting, which simplifies the list comprehension and serves as a cleaner type guard.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Summary
The Anthropic API requires that assistant text content appears before tool_use blocks in the message history, especially when integrating with Dify which stores assistant text and tool calls separately. Previously, text content was appended after tool calls, violating this requirement and potentially causing errors.
This commit refactors the message creation logic to ensure assistant prose is always placed before tool_use content. It introduces a helper method
_create_assistant_text_contentsto handle both string and structured text content, ensuring correct ordering according to Anthropic's tool-use guidelines.Change Type
Screenshots / Videos
LLM Plugin Checklist
Areas affected by this change (check all that apply)
Version
versioninmanifest.yaml(not the one undermeta)dify_plugin>=0.3.0,<0.6.0is declared inpyproject.tomland locked inuv.lock(or kept inrequirements.txtfor legacy plugins withoutuv.lock) — SDK docsTesting