Skip to content

fix(anthropic): fix Anthropic tool call message ordering for Dify compatibility#3275

Merged
crazywoola merged 2 commits into
langgenius:mainfrom
QIN2DIM:fix-tool-chains
Jun 11, 2026
Merged

fix(anthropic): fix Anthropic tool call message ordering for Dify compatibility#3275
crazywoola merged 2 commits into
langgenius:mainfrom
QIN2DIM:fix-tool-chains

Conversation

@QIN2DIM

@QIN2DIM QIN2DIM commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

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_contents to handle both string and structured text content, ensuring correct ordering according to Anthropic's tool-use guidelines.

Change Type

  • Documentation / non-plugin change
  • Non-LLM plugin (tools, extensions, datasource, etc.)
  • LLM plugin

Screenshots / Videos

Before After

LLM Plugin Checklist

Areas affected by this change (check all that apply)
  • Message flow (system messages, user ↔ assistant turn-taking)
  • Tool interaction flow (multi-round usage, Agent App and Agent Node)
  • Multimodal input (images, PDFs, audio, video, etc.)
  • Multimodal output (images, audio, video, etc.)
  • Structured output (JSON, XML, etc.)
  • Token consumption metrics
  • Other LLM functionality (reasoning, grounding, prompt caching, etc.)
  • New models / model parameter fixes

Version

  • Bumped top-level version in manifest.yaml (not the one under meta)
  • dify_plugin>=0.3.0,<0.6.0 is declared in pyproject.toml and locked in uv.lock (or kept in requirements.txt for legacy plugins without uv.lock) — SDK docs

Testing

  • Local deployment — Dify version:
  • SaaS (cloud.dify.ai)

…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.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 11, 2026
@QIN2DIM QIN2DIM temporarily deployed to models/anthropic June 11, 2026 06:44 — with GitHub Actions Inactive

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread models/anthropic/models/llm/llm.py
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Jun 11, 2026
@QIN2DIM QIN2DIM deployed to models/anthropic June 11, 2026 06:51 — with GitHub Actions Active
@crazywoola crazywoola merged commit 24e47c0 into langgenius:main Jun 11, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants