-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
fix(anthropic): normalize sampling params under extended thinking (temperature/top_p) #3780
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ec78fe3
420d62b
ce61169
1b1b934
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,16 +2,32 @@ | |||||||||||||||||||||||||||||||||||||||
| * Claude Code API constraints. | ||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||
| * Enforces Anthropic API requirements that real Claude Code handles: | ||||||||||||||||||||||||||||||||||||||||
| * 1. temperature=1 when thinking is enabled | ||||||||||||||||||||||||||||||||||||||||
| * 1. Sampling params under extended thinking: temperature=1 and top_p>=0.95 | ||||||||||||||||||||||||||||||||||||||||
| * (or unset) when thinking is enabled/adaptive | ||||||||||||||||||||||||||||||||||||||||
| * 2. Disable thinking when tool_choice forces a specific tool | ||||||||||||||||||||||||||||||||||||||||
| * 3. Enforce max 4 cache_control breakpoints | ||||||||||||||||||||||||||||||||||||||||
| * 4. Normalize cache_control TTL ordering | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||
| * Anthropic's extended-thinking contract rejects non-default sampling params: | ||||||||||||||||||||||||||||||||||||||||
| * with thinking enabled/adaptive, `temperature` may only be 1 and `top_p` must | ||||||||||||||||||||||||||||||||||||||||
| * be >= 0.95 (or unset) — otherwise the Messages API returns HTTP 400 | ||||||||||||||||||||||||||||||||||||||||
| * ("`temperature` may only be set to 1 ..." / "`top_p` must be greater than or | ||||||||||||||||||||||||||||||||||||||||
| * equal to 0.95 ..."). Clients such as the VS Code Copilot "Ollama" provider | ||||||||||||||||||||||||||||||||||||||||
| * routinely send other values (e.g. temperature 0.7, top_p 0.9), and thinking | ||||||||||||||||||||||||||||||||||||||||
| * can be injected by per-model requestDefaults *after* the request is built, so | ||||||||||||||||||||||||||||||||||||||||
| * normalize here: pin temperature to 1 and drop top_p (Anthropic's "unset" | ||||||||||||||||||||||||||||||||||||||||
| * branch — which also preserves the "never send both temperature and top_p" | ||||||||||||||||||||||||||||||||||||||||
| * invariant). | ||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||
| export function enforceThinkingTemperature(body: Record<string, unknown>): void { | ||||||||||||||||||||||||||||||||||||||||
| const thinking = body.thinking as Record<string, unknown> | undefined; | ||||||||||||||||||||||||||||||||||||||||
| if (thinking?.type === "enabled" || thinking?.type === "adaptive") { | ||||||||||||||||||||||||||||||||||||||||
| body.temperature = 1; | ||||||||||||||||||||||||||||||||||||||||
| if (body.top_p !== undefined) { | ||||||||||||||||||||||||||||||||||||||||
| delete body.top_p; | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
24
to
32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make
Suggested change
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
transformedBodyis null, undefined, or not an object, casting it toRecord<string, unknown>and passing it toenforceThinkingTemperaturewill cause a runtime crash (TypeError) when accessingbody.thinking. We should add a defensive check to ensuretransformedBodyis a valid non-null object before callingenforceThinkingTemperature.