Repository navigation
fix: omit sampling parameters for models that reject them (Opus 4.7+) - #25
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Thanks, @saarnilauri! I'm curious for your perspective on something. When @felixarntz and I were building these, we deliberately allowed for the API calls to break things in the case that the API wasn't being used properly. In this case, the dev is trying to pass invalid parameters to the model. So that's not a bug, that's an invalid use of the API. Why wouldn't the dev simply stop using those when using these models? If the issue is clearing up that these aren't supported for 4.7+, why wouldn't we make it clear that said models don't support that so the model selection feature in the AI Client doesn't select them? If the dev needs sampling adjustment, then perhaps those aren't the right models to use. What do you think? |
Beginning with Claude Opus 4.7, the Anthropic API returns a 400 error when 'temperature', 'top_p', or 'top_k' are set to any non-default value. The same applies to the Claude 5 generation models (e.g. Claude Fable 5). The model metadata previously advertised these options as supported for every model, so the AI Client's model selection would route prompts requiring a sampling parameter to models that reject it. The supported options are now built per model: sampling parameters are omitted for Claude Opus 4.7+ and for any model at major version 5 or higher. Model selection then routes such prompts to models that accept them (e.g. Claude Sonnet 4.6), and an explicitly selected model that rejects them fails loudly with the API's own descriptive 400 instead of silently misbehaving.
6796fcc to
cf3f04f
Compare
|
Thanks @JasonTheAdams that's a fair challenge, and after digging more deeply into how the SDK actually behaves, I agree with you. I've reworked the PR accordingly. What changed: the silent send-time stripping is gone. Instead, the metadata directory now builds the supported options per model and simply doesn't advertise
One correction to my original PR description: I claimed that removing the options from metadata would make the The one thing that initially pushed me toward dropping the params at send time: when a required option excludes every candidate, the SDK reports it as a capability failure. A question that came out of this for you: do you want explicit-model validation back in the |
|
I will be on leave for some time. Will be officially back at 20th of July. |
|
@JasonTheAdams I am back at work. Have you had time to look at this? |
JasonTheAdams
left a comment
There was a problem hiding this comment.
Thanks! I think this is a sensible approach. 👍
What?
Stop advertising the sampling parameters (
temperature,top_p,top_k) as supported options in the model metadata for Claude models that reject them, so the AI Client's model selection routes prompts that set a sampling parameter to models that actually honor it.Why?
Beginning with Claude Opus 4.7, the Anthropic API returns a
400error whentemperature,top_p, ortop_kare set to any non-default value. The same applies to the Claude 5 generation models (e.g. Claude Fable 5 / Claude Mythos 5). See the Claude model migration guide:Today the provider hardcodes
temperature/top_p/top_kas supported options for every model. That breaks both ways the AI Client can be used:using_temperature( 0.7 )can be routed to Claude Opus 4.7+ — the metadata claims support — and the request then fails with a400, even though models that honor the parameter (e.g. Claude Sonnet 4.6) were available.using_model( $opus_4_8_model )->using_temperature( 0.7 )fails with a400, but the SDK'sisSupported*()checks could not have warned the developer beforehand, because the metadata says the option is supported.How?
The supported options are now built per model in
AnthropicModelMetadataDirectory::parseResponseToModelMetadataList(), the same way the existing Claude 3 web-search distinction already works:modelRejectsSamplingParameters( string $modelId ): boolhelper parses the Claude model family and major/minor version and returnstruefor Claude Opus 4.7 and later, and for any model at major version 5 or higher (Claude Fable 5, Claude Mythos 5, and future 5.x models of any family). Sonnet 4.6, Haiku 4.5, earlier Opus versions, and the Claude 3 family are unaffected.temperature,top_p, andtop_koptions are simply omitted from the advertised supported options. No request payloads are modified.With truthful metadata, both usage paths behave correctly:
400("temperature is not supported for this model"), thePHP Ai ClientSDK does not validate explicitly selected models against metadata, so existing explicit-model code is unaffected by this change.Design notes:
400errors. Anthropic's migration guide makes clear that removing sampling parameters is the direction of travel.claude-opus-4-20250514, i.e. Claude Opus 4.0) are not misparsed as having minor version20250514.Scope note: this PR addresses the Opus 4.7+ / Claude 5 "sampling parameters rejected entirely" change. The separate Opus 4.1–4.6 / Sonnet 4.5 constraint (where
temperatureandtop_pmay not be set together, but either alone is fine) is intentionally left out of scope, as it requires a policy decision about which parameter to drop and cannot be expressed in the current supported-options metadata.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.8, Claude Fable 5
Used for: Investigating the root cause across the provider, the PHP AI Client SDK, and the Anthropic migration guide; drafting the implementation. The final implementation was reviewed and edited by me.
Testing Instructions
400.temperature/top_p/top_kare absent for Claude Opus 4.7+ and Claude 5 models, and still present for Claude Sonnet 4.6, Claude Haiku 4.5, and Claude Opus 4.6 and earlier.400(unchanged, intended behavior for invalid use):Changelog Entry