Skip to content

feat(messages): add client-side validation for temperature, top_p, and top_k#1136

Open
Ashutosh0x wants to merge 1 commit intoanthropics:mainfrom
Ashutosh0x:fix/temperature-validation
Open

feat(messages): add client-side validation for temperature, top_p, and top_k#1136
Ashutosh0x wants to merge 1 commit intoanthropics:mainfrom
Ashutosh0x:fix/temperature-validation

Conversation

@Ashutosh0x
Copy link

feat(messages): add client-side validation for temperature, top_p, and top_k

This PR adds client-side validation for sampling parameters to ensure they are within the ranges expected by the API, resolving #893.

Changes

  • Added a validate_sampling_params helper function to enforce:
    • temperature: 0.0 to 1.0
    • top_p: 0.0 to 1.0
    • top_k: >= 0
  • Integrated validation into Messages.create() and Messages.stream() (sync and async).
  • Integrated validation into Beta Messages.create(), Messages.parse(), Messages.stream(), and Messages.tool_runner() (sync and async).

Verification

Verified with a reproduction script testing both standard and beta resources with various invalid values. All tests passed, raising ValueError as expected instead of sending invalid requests to the API.

# Example of failure
client.messages.create(
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
    model="claude-3-opus-20240229",
    temperature=-1.0
)
# Raises: ValueError: Invalid temperature: -1.0; must be between 0.0 and 1.0

@Ashutosh0x Ashutosh0x requested a review from a team as a code owner January 21, 2026 20:51
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.

1 participant