Skip to content

Anthropic provider sends both temperature and top_p parameters causing API errors with newer Claude models #562

Description

@dcgrove

Checklist

  • I'm running the newest version of LLM Vision https://github.com/valentinfrlch/ha-llmvision/releases/latest
  • I have enabled debug logging for the integration.
  • I have filled out the issue template to the best of my ability.
  • This issue only contains 1 issue (if you have multiple issues, open one issue for each issue).
  • This is a bug and not a feature request.
  • I have searched open issues for my problem.

Describe the issue

Bug Description
The Anthropic provider in providers.py sends both temperature and top_p parameters in API requests, which causes errors with newer Claude models (Claude 3.5 Haiku, Claude Opus 4, etc.) that don't allow both parameters to be specified simultaneously.

Error Message
ERROR (MainThread) [custom_components.llmvision.providers] Provider Anthropic failed: invalid_request_error: `temperature` and `top_p` cannot both be specified for this model. Please use only one.

Environment
Home Assistant Core Version: 2025.12.0
LLM Vision Version: [Latest from main]
Provider: Anthropic
Model: claude-haiku-4-5 (also affects claude-3-5-haiku-latest, claude-opus-4-0, etc.)

Root Cause
In custom_components/llmvision/providers.py, the Anthropic class includes both temperature and top_p in the payload for both vision and text requests:

Lines 622-629 (_prepare_vision_data):
payload = {
    "model": self.model,
    "messages": [{"role": "user", "content": []}],
    "max_tokens": call.max_tokens,
    "temperature": default_parameters.get("temperature"),
    "top_p": default_parameters.get("top_p"),  # ← This causes the error
}
Lines 664-676 (_prepare_text_data):
return {
    "model": self.model,
    "messages": [
        {"role": "user", "content": [{"type": "text", "text": title_prompt}]},
        {"role": "user", "content": [{"type": "text", "text": call.message}]},
    ],
    "max_tokens": call.max_tokens,
    "temperature": default_parameters.get("temperature"),
    "top_p": default_parameters.get("top_p"),  # ← This causes the error
}

According to Anthropic's API documentation, newer models require choosing either temperature or top_p, not both.

Proposed Solution
Remove top_p from the Anthropic provider's payload. The temperature parameter should be sufficient for most use cases, and users can adjust it via the integration settings. Modified _prepare_vision_data:

payload = {
    "model": self.model,
    "messages": [{"role": "user", "content": []}],
    "max_tokens": call.max_tokens,
    "temperature": default_parameters.get("temperature"),
}
Modified _prepare_text_data:
return {
    "model": self.model,
    "messages": [
        {"role": "user", "content": [{"type": "text", "text": title_prompt}]},
        {"role": "user", "content": [{"type": "text", "text": call.message}]},
    ],
    "max_tokens": call.max_tokens,
    "temperature": default_parameters.get("temperature"),
}

Alternative Solution
If both parameters are needed for different models, implement model-specific logic:

payload = {
    "model": self.model,
    "messages": [{"role": "user", "content": []}],
    "max_tokens": call.max_tokens,
}

# Only add temperature for newer models, top_p for older ones
if self.model.startswith("claude-3-5") or self.model.startswith("claude-opus-4"):
    payload["temperature"] = default_parameters.get("temperature")
else:
    payload["temperature"] = default_parameters.get("temperature")
    payload["top_p"] = default_parameters.get("top_p")

Impact
All automations using Anthropic provider with newer Claude models fail silently
No AI analysis is performed, notifications may not be sent or contain error messages
Users may not realize the automation is broken until checking logs
Workaround
Currently, users can manually edit /config/custom_components/llmvision/providers.py and remove the top_p parameter from both methods, then restart Home Assistant.

Reproduction steps

Configure an Anthropic provider with a newer Claude model (e.g., claude-haiku-4-5)
Set up an automation using the AI Event Summary blueprint
Trigger the automation (e.g., via motion detection)
Check Home Assistant logs

Debug logs

Debug log of post fix automation working.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions