[1.x] Honor the Strict attribute on Mistral and OpenRouter tools

Merged
ME
4d ago
+127-1

#[Strict] on a tool currently only reaches OpenAI, xAI and Anthropic (#1219). Mistral and OpenRouter both accept strict on Chat Completions function definitions, but the shared MapsChatCompletionTools trait never sends it.

Changes

  • MapsChatCompletionTools gets a supportsStrictTools() switch, false by default. When it returns true and the tool has #[Strict], the function definition gets strict: true.
  • MistralGateway and OpenRouterGateway turn the switch on.
  • Tools without the attribute go out exactly as before, with no strict key.
  • Groq, Cohere, DeepSeek and OpenAI-compatible gateways are unchanged: Groq documents strict only on response_format, Cohere uses a top-level strict_tools flag, and DeepSeek requires the https://api.deepseek.com/beta base URL for strict mode.
#[Strict]
class LookupOrder implements Tool { /* every field ->required() */ }

agent(tools: [new LookupOrder])->prompt('Where is order 42?', provider: 'mistral');

OpenRouter and Claude models

OpenRouter forwards strict to models that support it. For Claude models it also needs the x-anthropic-beta: structured-outputs-2025-11-13 header: "Without this header, OpenRouter will strip the strict field and route normally" (provider selection). Apps can already send it through the provider's headers config, so this PR doesn't add it automatically.

// config/ai.php
'openrouter' => [
    'driver' => 'openrouter',
    'key' => env('OPENROUTER_API_KEY'),
    'headers' => ['x-anthropic-beta' => 'structured-outputs-2025-11-13'],
],

Behaviour change

Tools that already carry #[Strict] now send strict: true on Mistral and OpenRouter. OpenAI models reached through OpenRouter apply OpenAI's strict rules, so a strict tool with an optional field is rejected there, the same as on the OpenAI provider today.

Live check

  • Mistral (mistral-small-latest): strict tool accepted, called with {"min":1,"max":10}. A strict tool with an optional field is also accepted.
  • OpenRouter: pending. The test key hit its spend limit (403) before the run.

Tests

  • Mistral and OpenRouter: a #[Strict] tool sends the full function definition with strict: true; a tool without the attribute has no strict key.
  • Groq: a #[Strict] tool still has no strict key.