llamaIndex - โœ…(Solved) Fix [Bug]: Unexpected keyword argument `'tool_choice'` for `OpenAILike` used as `StructuredLLM` [2 pull requests, 5 comments, 3 participants]

Official PRs (โ€ฆ)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful ยท Quick feedback

Loadingโ€ฆ
GitHub stats
run-llama/llama_index#20790โ€ขFetched 2026-04-08 00:30:55
View on GitHub
Comments
5
Participants
3
Timeline
13
Reactions
0
Timeline (top)
commented ร—4cross-referenced ร—2labeled ร—2mentioned ร—2

Error Message

Traceback (most recent call last): File "/home/robebs/gitlab/nlp/llama_indexx.py", line 43, in <module> response = sllm.complete(text) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 435, in wrapped_llm_predict f_return_val = f(_self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/structured_llm.py", line 95, in complete return complete_fn(prompt, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/base/llms/generic_utils.py", line 184, in wrapper chat_response = func(messages, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 175, in wrapped_llm_chat f_return_val = f(_self, messages, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/structured_llm.py", line 63, in chat output = self.llm.structured_predict( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 1119, in structured_predict return super().structured_predict( ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/llm.py", line 360, in structured_predict result = program(llm_kwargs=llm_kwargs, **prompt_args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/program/llm_program.py", line 98, in call response = self._llm.complete(formatted_prompt, **llm_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai_like/base.py", line 168, in complete return super().complete(prompt, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper result = func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 435, in wrapped_llm_predict f_return_val = f(_self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 428, in complete return complete_fn(prompt, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 114, in wrapper return retry(f)(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/init.py", line 331, in wrapped_f return copy(f, *args, **kw) ^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/init.py", line 470, in call do = self.iter(retry_state=retry_state) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/init.py", line 371, in iter result = action(retry_state) ^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/init.py", line 393, in <lambda> self._add_action_func(lambda rs: rs.outcome.result()) ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/concurrent/futures/_base.py", line 449, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/init.py", line 473, in call result = fn(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 605, in _complete response = client.completions.create( ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/openai/_utils/_utils.py", line 286, in wrapper return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ TypeError: Completions.create() got an unexpected keyword argument 'tool_choice'

Fix Action

Fix / Workaround

Traceback (most recent call last):
  File "/home/robebs/gitlab/nlp/llama_indexx.py", line 43, in <module>
    response = sllm.complete(text)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 435, in wrapped_llm_predict
    f_return_val = f(_self, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/structured_llm.py", line 95, in complete
    return complete_fn(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/base/llms/generic_utils.py", line 184, in wrapper
    chat_response = func(messages, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 175, in wrapped_llm_chat
    f_return_val = f(_self, messages, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/structured_llm.py", line 63, in chat
    output = self.llm.structured_predict(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 1119, in structured_predict
    return super().structured_predict(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/llm.py", line 360, in structured_predict
    result = program(llm_kwargs=llm_kwargs, **prompt_args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/program/llm_program.py", line 98, in __call__
    response = self._llm.complete(formatted_prompt, **llm_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai_like/base.py", line 168, in complete
    return super().complete(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 435, in wrapped_llm_predict
    f_return_val = f(_self, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 428, in complete
    return complete_fn(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 114, in wrapper
    return retry(f)(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 331, in wrapped_f
    return copy(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 470, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 371, in iter
    result = action(retry_state)
             ^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 393, in <lambda>
    self._add_action_func(lambda rs: rs.outcome.result())
                                     ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 473, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 605, in _complete
    response = client.completions.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/openai/_utils/_utils.py", line 286, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
TypeError: Completions.create() got an unexpected keyword argument 'tool_choice'

PR fix notes

PR #20819: fix(openai-like): strip chat-only kwargs in completion fallback path

Description (problem / solution / changelog)

Summary

Fixes #20790

When OpenAILike is configured with is_function_calling_model=False and is_chat_model=False, the chat() method falls back to the text completions API. However, upstream callers like StructuredLLM pass chat-only kwargs (tool_choice, tools, etc.) that the completions API does not accept, causing:

TypeError: Completions.create() got an unexpected keyword argument 'tool_choice'

Root Cause

OpenAILike.chat() converts chat calls to complete() when is_chat_model=False, but passes through all **kwargs unfiltered. The text completions API (client.completions.create()) doesn't accept chat-specific parameters like tool_choice, tools, response_format, or parallel_tool_calls.

Fix

Added _CHAT_ONLY_KWARGS tuple and _strip_chat_kwargs() helper to filter out chat-specific kwargs before the completions fallback. Applied in all four methods:

  • chat()
  • stream_chat()
  • achat()
  • astream_chat()

The stripping only occurs in the completion fallback path (when is_chat_model=False). When is_chat_model=True, kwargs are passed through to super().chat() unchanged.

Testing

  • All 4 existing tests pass
  • The fix is minimal and surgical โ€” no behavior change for chat-mode users

Changed files

  • llama-index-integrations/llms/llama-index-llms-openai-like/llama_index/llms/openai_like/base.py (modified, +22/-4)

PR #20821: fix(openai): strip chat-only params from completions endpoint

Description (problem / solution / changelog)

Problem

When OpenAILike with is_function_calling_model=False is used as a StructuredLLM, structured_predict passes tool_choice through llm_kwargs to _complete(), which forwards it to client.completions.create(). The completions API does not support tools, tool_choice, or parallel_tool_calls, causing:

TypeError: Completions.create() got an unexpected keyword argument 'tool_choice'

Root Cause

_get_model_kwargs() merges all kwargs (including chat-only params) into a single dict. _complete() and _stream_complete() pass these directly to client.completions.create() without filtering.

Fix

Strip tools, tool_choice, and parallel_tool_calls in both _complete() and _stream_complete() before calling the completions endpoint.

Test

73 tests pass, 2 consecutive clean runs.

Fixes #20790

Changed files

  • llama-index-integrations/llms/llama-index-llms-openai/llama_index/llms/openai/base.py (modified, +8/-0)

Code Example

from llama_index.llms.openai_like import OpenAILike
client = OpenAILike(
    base_url=os.getenv("BASE_URL"),
    api_key=os.getenv("API_KEY"),
    http_client=httpx.Client(verify=False),
    model="gpt-oss-20b",
    is_function_calling_model=False
    
)
sllm = client.as_structured_llm(Invoice)
response = sllm.complete(text)
json_response = json.loads(response.text)
print(json.dumps(json_response, indent=2))

---

Traceback (most recent call last):
  File "/home/robebs/gitlab/nlp/llama_indexx.py", line 43, in <module>
    response = sllm.complete(text)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 435, in wrapped_llm_predict
    f_return_val = f(_self, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/structured_llm.py", line 95, in complete
    return complete_fn(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/base/llms/generic_utils.py", line 184, in wrapper
    chat_response = func(messages, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 175, in wrapped_llm_chat
    f_return_val = f(_self, messages, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/structured_llm.py", line 63, in chat
    output = self.llm.structured_predict(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 1119, in structured_predict
    return super().structured_predict(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/llm.py", line 360, in structured_predict
    result = program(llm_kwargs=llm_kwargs, **prompt_args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/program/llm_program.py", line 98, in __call__
    response = self._llm.complete(formatted_prompt, **llm_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai_like/base.py", line 168, in complete
    return super().complete(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 435, in wrapped_llm_predict
    f_return_val = f(_self, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 428, in complete
    return complete_fn(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 114, in wrapper
    return retry(f)(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 331, in wrapped_f
    return copy(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 470, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 371, in iter
    result = action(retry_state)
             ^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 393, in <lambda>
    self._add_action_func(lambda rs: rs.outcome.result())
                                     ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 473, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 605, in _complete
    response = client.completions.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/openai/_utils/_utils.py", line 286, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
TypeError: Completions.create() got an unexpected keyword argument 'tool_choice'
RAW_BUFFERClick to expand / collapse

Bug Description

Error when running a OpenAILike LLM as a StructuredLLM which raises a Unexpected keyword argument 'tool_choice' for OpenAILike used as StructuredLLM

Version

llama-index-core>=0.14.15 llama-index-llms-openai-like>=0.6.0

Steps to Reproduce

from llama_index.llms.openai_like import OpenAILike
client = OpenAILike(
    base_url=os.getenv("BASE_URL"),
    api_key=os.getenv("API_KEY"),
    http_client=httpx.Client(verify=False),
    model="gpt-oss-20b",
    is_function_calling_model=False
    
)
sllm = client.as_structured_llm(Invoice)
response = sllm.complete(text)
json_response = json.loads(response.text)
print(json.dumps(json_response, indent=2))

Relevant Logs/Tracbacks

Traceback (most recent call last):
  File "/home/robebs/gitlab/nlp/llama_indexx.py", line 43, in <module>
    response = sllm.complete(text)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 435, in wrapped_llm_predict
    f_return_val = f(_self, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/structured_llm.py", line 95, in complete
    return complete_fn(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/base/llms/generic_utils.py", line 184, in wrapper
    chat_response = func(messages, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 175, in wrapped_llm_chat
    f_return_val = f(_self, messages, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/structured_llm.py", line 63, in chat
    output = self.llm.structured_predict(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 1119, in structured_predict
    return super().structured_predict(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/llm.py", line 360, in structured_predict
    result = program(llm_kwargs=llm_kwargs, **prompt_args)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/program/llm_program.py", line 98, in __call__
    response = self._llm.complete(formatted_prompt, **llm_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai_like/base.py", line 168, in complete
    return super().complete(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index_instrumentation/dispatcher.py", line 335, in wrapper
    result = func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/core/llms/callbacks.py", line 435, in wrapped_llm_predict
    f_return_val = f(_self, *args, **kwargs)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 428, in complete
    return complete_fn(prompt, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 114, in wrapper
    return retry(f)(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 331, in wrapped_f
    return copy(f, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 470, in __call__
    do = self.iter(retry_state=retry_state)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 371, in iter
    result = action(retry_state)
             ^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 393, in <lambda>
    self._add_action_func(lambda rs: rs.outcome.result())
                                     ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/concurrent/futures/_base.py", line 449, in result
    return self.__get_result()
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    raise self._exception
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/tenacity/__init__.py", line 473, in __call__
    result = fn(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/llama_index/llms/openai/base.py", line 605, in _complete
    response = client.completions.create(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robebs/gitlab/nlp/.venv/lib/python3.12/site-packages/openai/_utils/_utils.py", line 286, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
TypeError: Completions.create() got an unexpected keyword argument 'tool_choice'

extent analysis

Quick Fix

OpenAILike.as_structured_llm() assumes the underlying model supports functionโ€‘calling.
Because you created the client with is_function_calling_model=False, the wrapper still adds the tool_choice argument (used only for chatโ€‘completion with function calling).
The OpenAIโ€‘compatible server you are hitting does not accept that argument, so Completions.create() raises:

TypeError: Completions.create() got an unexpected keyword argument 'tool_choice'

Two ways to solve it

OptionWhen to useWhat to change
A. Enable functionโ€‘calling on the clientYour model actually supports function calls (e.g. OpenAIโ€‘compatible chat endpoint).Set is_function_calling_model=True (or omit the flag, the default is True).
B. Disable toolโ€‘choice for nonโ€‘functionโ€‘calling modelsYour model only supports plain completions and cannot do function calls.Tell the wrapper not to send tool_choice by passing tool_choice="none" (or upgrade to a newer llama-index where the flag is respected).

Both approaches stop the illegal tool_choice kwโ€‘arg from being sent.


Example Code โ€“ Optionโ€ฏA (recommended if the model supports it)

import os, json, httpx
from llama_index.llms.openai_like import OpenAILike
from llama_index.core.llms import StructuredLLM  # for typing only

# ------------------------------------------------------------------
# 1๏ธโƒฃ  Create the client **with functionโ€‘calling enabled**
# ------------------------------------------------------------------
client = OpenAILike(
    base_url=os.getenv("BASE_URL"),
    api_key=os.getenv("API_KEY"),
    http_client=httpx.Client(verify=False),
    model="gpt-oss-20b",
    # is_function_calling_model defaults to True โ€“ omit or set True
    is_function_calling_model=True,
)

# ------------------------------------------------------------------
# 2๏ธโƒฃ  Convert to a StructuredLLM (your Pydantic/Dataclass schema)
# ------------------------------------------------------------------
sllm: StructuredLLM = client.as_structured_llm(Invoice)   # replace Invoice with your schema

# ------------------------------------------------------------------
# 3๏ธโƒฃ  Run the request
# ------------------------------------------------------------------
text = "Please extract the invoice fields from the following text ..."
response = sllm.complete(text)

# ------------------------------------------------------------------
# 4๏ธโƒฃ  Inspect the JSON result
# ------------------------------------------------------------------
json_response = json.loads(response.text)
print(json.dumps(json_response, indent=2))

Example Code โ€“ Optionโ€ฏB (if the model cannot do function calls)

import os, json, httpx
from llama_index.llms.openai_like import OpenAILike

client = OpenAILike(
    base_url=os.getenv("BASE_URL"),
    api_key=os.getenv("API_KEY"),
    http_client=httpx.Client(verify=False),
    model="gpt-oss-20b",
    is_function_calling_model=False,          # keep as False
    tool_choice="none",                       # <-- suppress the unsupported kwarg
)

sllm = client.as_structured_llm(Invoice)
response = sllm.complete(text)
print(json.dumps(json.loads(response.text), indent=2))

Why tool_choice="none" works โ€“ the StructuredLLM wrapper adds tool_choice only when it is nonโ€‘None. Setting it to "none" tells the wrapper to omit the argument, matching the serverโ€™s completion API.


Verification Steps

  1. Run the script after applying one of the two fixes.
  2. The traceback should disappear; you should receive a normal Response object.
  3. response.text (or response.json()) should contain the structured output defined by your Invoice schema.

If you still see the same TypeError, doubleโ€‘check that the client instance you are using is the one you modified (no stale imports) and that the library versions are at least:

llama-index-core >= 0.14.15
llama-index-llms-openai-like >= 0.6.0

Extra Tips

  • Upgrade โ€“ newer llama-index releases (โ‰ฅโ€ฏ0.7.0 of llama-index-llms-openai-like) automatically suppress tool_choice when is_function_calling_model=False. Upgrading may remove the need for the manual tool_choice="none" flag.
  • Environment โ€“ ensure the server you point BASE_URL to actually implements the OpenAI chat endpoint if you intend to use function calling; otherwise stick with the plain completions endpoint.
  • Testing โ€“ write a tiny unit test that calls client.as_structured_llm and asserts that the request payload does not contain a tool_choice key when is_function_calling_model=False.
  • Futureโ€‘proofing โ€“ when switching to a model that does support function calls, simply set is_function_calling_model=True and remove the tool_choice="none" override. The same code will then automatically use the richer toolโ€‘calling flow.

Vote matrix ยท Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loadingโ€ฆ

Still need to ship something?

ร—6

Another batch ranked right after the header list โ€” different links, same matching logic.

Back to top recommendations

TRENDING

llamaIndex - โœ…(Solved) Fix [Bug]: Unexpected keyword argument `'tool_choice'` for `OpenAILike` used as `StructuredLLM` [2 pull requests, 5 comments, 3 participants]