Filter invalid parameters in Ollama client requests (#5983)

Remove unrecognized parameters in Ollama API calls.
---------

Co-authored-by: Eric Zhu <ekzhu@users.noreply.github.com>
This commit is contained in:
Federico Villa 2025-03-17 22:09:26 +01:00 committed by GitHub
parent c4e07e86d8
commit 09d8d344a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 2 deletions

View File

@ -82,14 +82,22 @@ ollama_chat_request_fields: dict[str, Any] = [m for m in inspect.getmembers(Chat
1
]
OLLAMA_VALID_CREATE_KWARGS_KEYS = set(ollama_chat_request_fields.keys()) | set(
("model", "messages", "tools", "stream", "format", "options", "keep_alive")
("model", "messages", "tools", "stream", "format", "options", "keep_alive", "response_format")
)
# NOTE: "response_format" is a special case that we handle for backwards compatibility.
# It is going to be deprecated in the future.
def _create_args_from_config(config: Mapping[str, Any]) -> Dict[str, Any]:
if "response_format" in config:
warnings.warn(
"Using response_format will be deprecated. Use json_output instead.",
DeprecationWarning,
stacklevel=2,
)
create_args = {k.lower(): v for k, v in config.items() if k.lower() in OLLAMA_VALID_CREATE_KWARGS_KEYS}
dropped_keys = [k for k in config.keys() if k.lower() not in OLLAMA_VALID_CREATE_KWARGS_KEYS]
logger.info(f"Dropped the following unrecognized keys from create_args: {dropped_keys}")
trace_logger.info(f"Dropped the following unrecognized keys from create_args: {dropped_keys}")
return create_args
# create_args = {k: v for k, v in config.items() if k in create_kwargs}
@ -411,6 +419,7 @@ class BaseOllamaChatCompletionClient(ChatCompletionClient):
# Copy the create args and overwrite anything in extra_create_args
create_args = self._create_args.copy()
create_args.update(extra_create_args)
create_args = _create_args_from_config(create_args)
response_format_value: JsonSchemaValue | Literal["json"] | None = None