Skip to content

Conversation

@ping-Toven
Copy link

@ping-Toven ping-Toven commented Dec 16, 2025

Description

Adds support for Anthropic's strict tool use feature, which validates tool parameters to ensure Claude calls functions with correctly-typed arguments. This is useful for building reliable agentic workflows where type-safe function calls are critical.

When strictToolUse: true is set in chat settings:

  • Adds strict: true to each tool definition in the request body
  • Includes the required anthropic-beta: structured-outputs-2025-11-13 header

Usage:

const model = provider.chat('anthropic/claude-3.5-sonnet', {
  strictToolUse: true,
});

Reference: https://docs.anthropic.com/en/docs/build-with-claude/tool-use/strict-tool-use

Implementation Details

  • Added strictToolUse?: boolean setting to OpenRouterChatSettings
  • Added getStrictToolUseHeaders() helper method for beta header injection
  • Modified tool mapping in getArgs() to conditionally include strict: true
  • Updated both doGenerate() and doStream() to include headers when enabled
  • Added 9 new unit tests covering both streaming and non-streaming scenarios

Notes

  • The feature is opt-in (defaults to false), so existing behavior is unchanged
  • The anthropic-beta header is sent whenever strictToolUse is enabled, even if no tools are in the request

Checklist

  • I have run pnpm stylecheck and pnpm typecheck
  • I have run pnpm test and all tests pass
  • I have added tests for my changes (if applicable)
  • I have updated documentation (if applicable)

Changeset

  • I have run pnpm changeset to create a changeset file

Note: A changeset is required for your changes to trigger a release. If your PR only contains docs, tests, or CI changes that don't need a release, run pnpm changeset --empty instead.

Review & Testing Checklist for Human

  • Verify strict: true placement: The property is added at the tool level (alongside type: 'function'), not inside the function object. Confirm this matches OpenRouter's expected format for Anthropic models.
  • E2E test with actual API: Unit tests use mocked servers. Test with a real Anthropic model through OpenRouter to verify the feature works end-to-end.
  • Verify beta header is current: The hardcoded header value structured-outputs-2025-11-13 should be checked against current Anthropic docs.

Recommended test plan:

  1. Create a simple script using the provider with strictToolUse: true
  2. Call an Anthropic model (e.g., anthropic/claude-3.5-sonnet) with a tool that has strict schema requirements
  3. Verify the model returns correctly-typed arguments matching the schema

Requested by: Tomas Oliva (@ping-Toven)
Devin session: https://app.devin.ai/sessions/b201995bb0ef4d1f8fa8a6531de0f6fb

- Add strictToolUse setting to OpenRouterChatSettings
- When enabled, adds strict: true to each tool definition
- Includes anthropic-beta: structured-outputs-2025-11-13 header
- Add comprehensive unit tests for doGenerate and doStream

Co-Authored-By: Tomas Oliva <tomasoliva@outlook.com>
@devin-ai-integration
Copy link
Contributor

Original prompt from Tomas
@Devin add support for anthropic's strict tool use with full unit test coverage.
Thread URL: https://openrouter.slack.com/archives/C07UF9XLTFF/p1765928745422299

@devin-ai-integration
Copy link
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR that start with 'DevinAI' or '@devin'.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

- Update test expectation to use tool.description instead of tool.type
- Keep both strict tool use tests and new tests from main

Co-Authored-By: Tomas Oliva <tomasoliva@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants