Skip to content

feat(anthropic): add anthropic_server_tools to AnthropicChatGenerator#3386

Open
Aftabbs wants to merge 2 commits into
deepset-ai:mainfrom
Aftabbs:feat/anthropic-server-tools
Open

feat(anthropic): add anthropic_server_tools to AnthropicChatGenerator#3386
Aftabbs wants to merge 2 commits into
deepset-ai:mainfrom
Aftabbs:feat/anthropic-server-tools

Conversation

@Aftabbs
Copy link
Copy Markdown
Contributor

@Aftabbs Aftabbs commented Jun 3, 2026

Summary

Closes #2724

AnthropicChatGenerator currently validates that all items in tools are Tool or Toolset instances, which prevents users from passing Anthropic's native server-side tools (web search, computer use, etc.). This PR adds an anthropic_server_tools parameter that accepts raw dicts and forwards them directly to the Anthropic API alongside any function tools.

Root Cause

Anthropic's messages.create API accepts two categories of tools:

  1. Custom function tools — validated and converted by haystack's Tool/Toolset machinery
  2. Built-in server-side tools (e.g. {"type": "web_search_20250305"}) — these bypass function-call dispatch and are handled entirely by the Anthropic backend

The existing tools parameter only supports category 1. Users who tried to pass raw dicts got a TypeError: Items in the tools list must be Tool or Toolset instances.

Changes

File What changed
chat_generator.py Added anthropic_server_tools: list[dict[str, Any]] | None = None parameter; stored, serialized, and appended to the tools list in _prepare_request_params
test_chat_generator.py Updated 3 existing serde tests to include the new param; added 5 new unit tests

Usage

from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator

generator = AnthropicChatGenerator(
    model="claude-sonnet-4-5",
    anthropic_server_tools=[{"type": "web_search_20250305"}],
)
result = generator.run(messages=[...])

Function tools and server-side tools can also be combined:

generator = AnthropicChatGenerator(
    tools=[my_function_tool],
    anthropic_server_tools=[{"type": "web_search_20250305"}],
)

Testing

  • Existing tests pass (updated test_to_dict_default, test_to_dict_with_parameters, test_serde_in_pipeline for new param)
  • New test: test_init_with_anthropic_server_tools — verifies parameter is stored
  • New test: test_to_dict_with_anthropic_server_tools — verifies serialization round-trip
  • New test: test_from_dict_with_anthropic_server_tools — verifies deserialization
  • New test: test_run_with_anthropic_server_tools — verifies server tools are passed to messages.create
  • New test: test_run_with_tools_and_anthropic_server_tools — verifies mixed function + server tools

Impact

Users can now use Anthropic's native web search and other built-in tools directly in Haystack pipelines without hacks. The parameter is fully serializable, so pipelines that use it can be saved and reloaded via pipeline.to_dict() / Pipeline.from_dict().

Allow users to pass Anthropic server-side (built-in) tools such as
web_search_20250305 directly to the API without being blocked by
the haystack Tool/Toolset type validation.

The new `anthropic_server_tools` parameter accepts a list of raw dicts
that are appended to any function tools before the messages.create call.
Serialization and deserialization are handled automatically via
default_to_dict/default_from_dict.

Fixes deepset-ai#2724
@Aftabbs Aftabbs requested a review from a team as a code owner June 3, 2026 07:30
@Aftabbs Aftabbs requested review from anakin87 and removed request for a team June 3, 2026 07:30
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 3, 2026

Heads-up for maintainers

This PR is from a fork and touches integrations whose integration tests require API keys.
Those tests are skipped in CI because fork PRs don't have access to repo secrets for security reasons.

Affected integrations:

  • anthropic

Please run the integration tests locally (hatch run test:integration inside each folder) before approving.

@github-actions github-actions Bot added the type:documentation Improvements or additions to documentation label Jun 3, 2026
@davidsbatista
Copy link
Copy Markdown
Contributor

@Aftabbs, I see you have three PRs open, I was reviewing one, which is still under review with failing tests and with changes requests. I kindly ask you to focus on that PR before opening new ones.

Copy link
Copy Markdown
Member

@anakin87 anakin87 left a comment

Choose a reason for hiding this comment

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

Thanks for this PR!

Tests are failing.
Please make them pass then ping me to get a review.

…hatGenerator

AnthropicFoundryChatGenerator inherits run() from AnthropicChatGenerator,
which calls _prepare_request_params() that references self.anthropic_server_tools.
Since the subclass __init__ did not declare or set this attribute, every call
to run() raised AttributeError.

Add anthropic_server_tools param to __init__, set self.anthropic_server_tools,
include it in to_dict(), and update the serde / run-triggers-warm-up tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration:anthropic type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for Anthropic native web_search tool

3 participants