Skip to content

fix(models): honor a single allowed_function_names entry in LiteLlm tool_choice - #8445

Open
abhishekacharya200 wants to merge 1 commit into
google:mainfrom
abhishekacharya200:fix/litellm-allowed-function-names
Open

abhishekacharya200 wants to merge 1 commit into
google:mainfrom
abhishekacharya200:fix/litellm-allowed-function-names

Conversation

@abhishekacharya200

Copy link
Copy Markdown

Please ensure you have read the contribution guide before creating a pull request.

Link to Issue or Description of Change

1. Link to an existing issue (if applicable):

  • Closes: N/A
  • Related: N/A

2. Or, if no issue exists, describe the change:

Problem:

Describe the bug
With FunctionCallingConfig(mode=ANY, allowed_function_names=["some_tool"]), LiteLlm always sends tool_choice="required" and drops the allowed name. The model is then free to call any declared tool instead of the one requested. AnthropicLlm already maps a single allowed name to a forced tool, and ForceEscalationPlugin relies on this behavior to force one specific tool.

To Reproduce

  1. Build an LlmRequest with two declared functions and tool_config set to FunctionCallingConfig(mode=ANY, allowed_function_names=["my_func"]).
  2. Call _get_completion_inputs(llm_request, model="openai/gpt-4o").
  3. The returned tool_choice is "required".

Expected behavior
tool_choice should be {"type": "function", "function": {"name": "my_func"}} so the provider is forced to call that tool.

Solution:
In the ANY branch of _get_completion_inputs, when exactly one allowed name is given and it matches a declared tool, send the function style tool_choice for that name. With several names, an unknown name, or no names, it still sends "required". The no tools handling is unchanged.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

Added two tests in tests/unittests/models/test_litellm.py: a single allowed name gives the forced function tool_choice, and two allowed names give "required". The single name test fails on main (assert 'required' == {'type': 'function', ...}) and passes with the fix.

tests/unittests/models/test_litellm.py: 2 failed, 890 passed

The 2 failures are test_get_completion_inputs_ignores_unsupported_reasoning_params (minimax and bedrock cases), which fail the same way on main with my installed litellm version and are unrelated.

Manual End-to-End (E2E) Tests:

I did not run it against a live model. I called _get_completion_inputs with the request from the steps above: before the change it returned "required", after it returns {"type": "function", "function": {"name": "my_func"}}.

Checklist

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules. (none)

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