Assertive-xxh opened a new issue, #1441:
URL: https://github.com/apache/flink-agents/issues/1441

   ### Search before asking
   
   - [x] I searched in the 
[issues](https://github.com/apache/flink-agents/issues) and found nothing 
similar.
   
   ### Description
   
   A Python function tool with an `Optional[...]` parameter reaches Ollama 
without that parameter's type.
   
   `FunctionSchema` writes `Optional[int]` as `"anyOf": [{"type": "integer"}, 
{"type": "null"}]`. `OllamaChatModelConnection.chat` converts the tools with 
`to_openai_tool` 
(python/flink_agents/integrations/chat_models/ollama_chat_model.py:222) and 
passes them to the `ollama` client (line 249). The client parses each property 
into `Tool.Function.Parameters.Property`, which only has `type`, `items`, 
`description` and `enum` (`ollama/_types.py` in ollama 0.6.1, the pinned 
version, and unchanged in 0.6.3). `anyOf` is dropped without an error, so the 
request only carries the description.
   
   Expected: the parameter is sent with its type, as the Java Ollama connection 
does since #1396.
   Actual: `"days": {"description": "Number of days."}`.
   
   The model is not told that `days` is an integer. If it sends `"3"`, 
`FunctionSchema.bind` rejects the arguments (`INVALID_ARGUMENT /days: anyOf`), 
and the model only gets the tool result ``Tool `get_weather` execute failed.``, 
which does not say why.
   
   - The Ollama server accepts both `anyOf` and a type list in a tool property 
([`ToolProperty`](https://github.com/ollama/ollama/blob/924cc286cc592f49c481fa84e15fa60dbc75989e/api/types.go#L426)),
 so the type is lost in the Python client, not on the server. I found no 
upstream issue or PR that adds `anyOf` to `Property`.
   - The Python OpenAI connection sends the `anyOf` unchanged (I captured its 
request), and the vLLM connection reuses it. The Anthropic, Azure OpenAI, 
DashScope and watsonx connections pass the same dict to their SDKs; I did not 
capture their requests.
   - #1395 says the Python Ollama connection "passes the schema through and is 
not affected". That holds for a `[<type>, "null"]` type list like Java's, but 
the Python `FunctionSchema` writes `anyOf`, and the client drops it. The 
"Nullable tool parameters (Java)" note in `providers.md` only covers Java.
   - The same client model also drops other properties that have no top-level 
`type`, such as a pydantic model parameter (`$ref`) or `Union[int, str]`. This 
issue only covers the nullable case, which is the Python side of #1395.
   
   ### Possible fix
   
   Before passing the tools to the client, the Python Ollama connection could 
rewrite a property of the form `{"anyOf": [<schema with one type>, {"type": 
"null"}], ...}` into that schema, keeping the property's own `description`. The 
client keeps `type`, `items` and `enum`, so `days` would be sent as `{"type": 
"integer", "description": "Number of days."}`. This is what the Java connection 
sends since #1396, and what ollama-python's own `convert_function_to_tool` does 
for a nullable parameter. A local prototype is under 20 lines and leaves 
`tool.metadata` unchanged.
   
   The client also accepts a type list, so Python could send `["integer", 
"null"]` and keep the null. I have not checked how model templates render a 
type list, so I would start with the non-null type, unless you prefer the list.
   
   ### How to reproduce
   
   ```python
   from typing import Optional
   
   from ollama import Tool as OllamaTool
   
   from flink_agents.integrations.chat_models.chat_model_utils import 
to_openai_tool
   from flink_agents.plan.function import PythonFunction
   from flink_agents.plan.tools.function_tool import FunctionTool
   
   
   def get_weather(city: str, days: Optional[int] = None) -> str:
       """Get the weather forecast.
   
       Args:
           city: City name.
           days: Number of days.
       """
       return "sunny"
   
   
   tool = FunctionTool(func=PythonFunction.from_callable(get_weather))
   print(tool.metadata.args_schema["properties"]["days"])
   
   # What OllamaChatModelConnection.chat passes as `tools`, after the client 
parses it.
   sent = OllamaTool.model_validate(to_openai_tool(metadata=tool.metadata))
   
print(sent.model_dump(exclude_none=True)["function"]["parameters"]["properties"])
   ```
   
   Output:
   
   ```
   {'anyOf': [{'type': 'integer'}, {'type': 'null'}], 'default': None, 
'description': 'Number of days.', 'title': 'Days'}
   {'city': {'type': 'string', 'description': 'City name.'}, 'days': 
{'description': 'Number of days.'}}
   ```
   
   I also pointed `OllamaChatModelConnection` at a local HTTP server and 
captured the request body. Its `tools` match the second line.
   
   ### Version and environment
   
   main at 0a646ef, Python 3.12, ollama 0.6.1 (the pinned version), WSL Ubuntu 
24.04. Not tested against a running Ollama server.
   
   ### Are you willing to submit a PR?
   
   - [x] I'm willing to submit a PR!
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to