Skip to content

Conversation

@alceasan
Copy link

Proposed change

Certain OpenRouter models, particularly Meta Llama versions, have issues with tool usage in Home Assistant's OpenRouter integration. These models often return tool calls in non-standard formats or fail to properly parse tool arguments, leading to errors or non-functional tools.

Root Cause

  1. The original implementation assumed tool calls would always be in the proper OpenAI-compatible format
  2. Tool argument parsing was fragile and didn't handle edge cases (e.g., bytes, None, nested JSON strings)
  3. Models like Meta Llama sometimes embed tool calls as JSON in the response content instead of using proper tool_call fields
  4. Tool availability wasn't being carried forward from previous conversation turns

Solution

This PR makes several improvements to ensure robust tool usage:

  1. Safer argument decoding: Updated _decode_tool_arguments to handle various input types gracefully with proper fallbacks and logging.

  2. Flexible tool call parsing: Modified _transform_response to detect tool calls both in the standard tool_calls field and as JSON embedded in the response content.

  3. Dynamic tool discovery: Added logic to scan conversation history and automatically include tools that have been used in previous turns, ensuring continuity in multi-turn conversations.

  4. Improved iteration handling: Enhanced the chat iteration loop to better process tool results and maintain tool context across exchanges.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

- Enhance _decode_tool_arguments to safely handle various argument types (None, bytes, dict, malformed strings)
- Expand _transform_response to support tool calls embedded in content JSON as fallback for stricter format models
- Add dynamic tool discovery from conversation history to ensure tool availability across chat iterations
- Improve tool call processing with validation and normalization for better model compatibility
@alceasan alceasan requested a review from joostlek as a code owner October 18, 2025 12:11
Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

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

Hi @alceasan

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant
Copy link

Hey there @joostlek, mind taking a look at this pull request as it has been labeled with an integration (open_router) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of open_router can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign open_router Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Use empty list fallback in _normalize_tools to handle None input in tool normalization when tools=None
Skip adding assistant content when no speech can be extracted from tool results, preventing raw JSON from being shown to users in conversation responses.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenRouter agent cannot invoke tools

2 participants