Skip to content

fix: omit sampling parameters for models that reject them (Opus 4.7+) - #25

Merged
JasonTheAdams merged 1 commit into
WordPress:trunkfrom
saarnilauri:improvement/model-supports-migration
Jul 20, 2026
Merged

JasonTheAdams merged 1 commit into
WordPress:trunkfrom
saarnilauri:improvement/model-supports-migration

Conversation

@saarnilauri

@saarnilauri saarnilauri commented May 29, 2026 •

Copy link
Copy Markdown
Contributor

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 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 / Claude Mythos 5). See the Claude model migration guide:

Sampling parameters removed: Setting temperature, top_p, or top_k to any non-default value on Claude Opus 4.7 returns a 400 error. The safest migration path is to omit these parameters entirely from request payloads.

Today the provider hardcodes temperature/top_p/top_k as supported options for every model. That breaks both ways the AI Client can be used:

  • Automatic model selection: a prompt using using_temperature( 0.7 ) can be routed to Claude Opus 4.7+ — the metadata claims support — and the request then fails with a 400, even though models that honor the parameter (e.g. Claude Sonnet 4.6) were available.
  • Explicit model selection: using_model( $opus_4_8_model )->using_temperature( 0.7 ) fails with a 400, but the SDK's isSupported*() 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:

  • A new modelRejectsSamplingParameters( string $modelId ): bool helper parses the Claude model family and major/minor version and returns true for 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.
  • For matching models, the temperature, top_p, and top_k options are simply omitted from the advertised supported options. No request payloads are modified.

With truthful metadata, both usage paths behave correctly:

  • Automatic selection routes sampling-parameter prompts to a model that supports them.
  • Explicit selection of a rejecting model still sends the parameter and fails loudly with Anthropic's own descriptive 400 ("temperature is not supported for this model"), the PHP Ai Client SDK does not validate explicitly selected models against metadata, so existing explicit-model code is unaffected by this change.

Design notes:

  • Why over-match unknown 5.x models: if a future 5.x model unexpectedly does accept sampling parameters, the cost is only that automatic selection routes around it; advertising support a model does not have would instead produce hard 400 errors. Anthropic's migration guide makes clear that removing sampling parameters is the direction of travel.
  • Version parsing: the minor-version capture is limited to two digits so that dated model IDs without a minor version (e.g. claude-opus-4-20250514, i.e. Claude Opus 4.0) are not misparsed as having minor version 20250514.
  • Companion SDK PR: when a required option excludes every candidate model, the SDK previously reported it as a capability failure ("No models found that support text_generation for this prompt"), which would be misleading for consumers. Distinguish unmet required options from unsupported capabilities in model selection errors php-ai-client#254 makes that error name the unmet options instead, e.g. "Models supporting text_generation are available, but none of them support the following required options: temperature."

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 temperature and top_p may 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

  1. Configure the Anthropic provider with a valid API key.
  2. Generate text with a non-default temperature without selecting a model explicitly:
    wp_ai_client_prompt( 'Write a haiku about WordPress.' )
        ->using_temperature( 0.7 )
        ->generate_text_result();
    • Before: model selection may pick Claude Opus 4.7+ and the request fails with a 400.
    • After: model selection picks a model that supports sampling parameters (e.g. Claude Sonnet 4.6) and the request succeeds with the temperature honored.
  3. Inspect the model metadata (e.g. via the provider's model metadata directory) and confirm temperature/top_p/top_k are 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.
  4. Explicitly select a rejecting model with a sampling parameter and confirm the request fails with Anthropic's descriptive 400 (unchanged, intended behavior for invalid use):
    wp_ai_client_prompt( 'Write a haiku about WordPress.' )
        ->using_temperature( 0.7 )
        ->using_model( $opus_4_8_model )
        ->generate_text_result();

Changelog Entry

Fixed - Stop advertising sampling parameters (temperature, top_p, top_k) as supported options for Claude models that reject them (Claude Opus 4.7+ and Claude 5 models), so model selection routes prompts using these options to models that honor them.

@github-actions

github-actions Bot commented May 29, 2026 •

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: saarnilauri <laurisaarni@git.wordpress.org>
Co-authored-by: JasonTheAdams <jason_the_adams@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@JasonTheAdams

Copy link
Copy Markdown
Member

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.
@saarnilauri

Copy link
Copy Markdown
Contributor Author

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 temperature/top_p/top_k for models that reject them (Claude Opus 4.7+, and the Claude 5 generation, Fable 5 rejects them too). With truthful metadata:

  • Model selection does its job. A prompt with using_temperature() and no explicit model gets routed to a model that actually honors it (e.g. Sonnet 4.6) instead of one that would either 400 or, with my earlier approach, silently ignore it.
  • Explicit invalid use fails loudly. using_model( $opus_4_8 )->using_temperature( 0.7 ) still reaches the API and gets Anthropic's own descriptive 400. That matches the design intent you described, and the API's message ("temperature is not supported for this model") tells the dev exactly what to fix.

One correction to my original PR description: I claimed that removing the options from metadata would make the PHP Ai Client reject using_temperature() client-side before the request. That's not actually true in the current PHP Ai Client. PromptBuilder::getConfiguredModel() uses an explicitly set model as-is without checking it against the requirements (the docblock still says it validates, but that was removed in theba25084 commit). So the cost of truthful metadata is lower than I feared: explicit-model code paths are unaffected, only discovery and isSupported*() consult the metadata.

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. isSupportedForTextGeneration() returns false, and discovery throws "No models found that support text_generation for this prompt." A dev who set a temperature would be told text generation isn't supported, which is misleading. Rather than work around that here, I've opened a PR against the SDK that makes the error name the unmet options when models supporting the capability exist, e.g. "Models supporting text_generation are available, but none of them support the following required options: temperature." It also fixes the stale getConfiguredModel() docblock: WordPress/php-ai-client#254

A question that came out of this for you: do you want explicit-model validation back in the PHP Ai Client(the pre-ba25084 behavior, which would turn the provider 400 into an earlier, equally explicit client-side exception), or is "explicit model = dev's responsibility, let the API answer" the intended contract? Either is fine for this PR. With truthful metadata both fail loudly but the docblock and behavior should agree, and I'd happily adjust the PHP Ai Client PR whichever way you prefer.

@saarnilauri

Copy link
Copy Markdown
Contributor Author

I will be on leave for some time. Will be officially back at 20th of July.

@saarnilauri

Copy link
Copy Markdown
Contributor Author

@JasonTheAdams I am back at work. Have you had time to look at this?

@JasonTheAdams JasonTheAdams left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! I think this is a sensible approach. 👍

@JasonTheAdams
JasonTheAdams merged commit d11ba1f into WordPress:trunk Jul 20, 2026
2 checks passed
@jeffpaul jeffpaul modified the milestones: 1.1.0, 1.0.4 Aug 17, 2026
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.

3 participants