Assertive-xxh opened a new pull request, #1442:
URL: https://github.com/apache/flink-agents/pull/1442

   Linked issue: #1441
   
   ### Purpose of change
   
   The Python Ollama connection sends tool schemas through the Ollama SDK, 
whose tool parameter model keeps only `type`, `items`, `description` and 
`enum`. A nullable parameter such as `days: int | None` has the schema `anyOf: 
[{"type": "integer"}, {"type": "null"}]`, so it reached the model without a 
type. Such a parameter is now sent with the type list `[<type>, "null"]`, which 
the SDK keeps.
   
   #### Runtime flow
   
   `OllamaChatModelConnection.chat` now builds each tool payload with 
`_to_ollama_tool`. It calls `to_openai_tool` as before, then passes each 
top-level property through `_nullable_as_type_list`. The payload is built from 
the copy that `get_parameters_dict` returns, so the tool metadata is not 
modified, and tool call arguments are still validated against the original 
schema.
   
   #### Key decisions
   
   - Only `anyOf: [S, {"type": "null"}]` is rewritten, where `S` has a single 
`type`. Python's `FunctionSchema` produces this for `X | None` when `X` is a 
primitive, a dict, a list, or a `Literal` whose values share one JSON type. A 
`$ref` (a model or an `Enum` class) and a union of several types are sent as 
before.
   - The keys of `S` (`enum`, `items`, ...) are merged into the property. If 
both define a key, the property's value is kept.
   - The result matches what Java's `FunctionSchema` writes for 
`@ToolParam(nullable = true)`: the type list, and `null` appended to an `enum`. 
Without that `null`, the `enum` would exclude a value that the type list and 
the framework accept.
   - Java's Ollama connection still sends only the non-null type, because 
ollama4j takes a single type name, as the providers page notes. This PR does 
not change Java.
   
   ### Behavioral Semantics
   
   #### Interaction decisions
   
   | Parameter | Sent to Ollama before | Sent now |
   |---|---|---|
   | `city: str` | `type: string` | unchanged |
   | `days: int \| None` (with or without a default) | no `type` | `type: 
[integer, null]` |
   | `unit: Literal["C", "F"] \| None` | no `type`, no `enum` | `type: [string, 
null]`, `enum: [C, F, null]` |
   | `tags: List[str] \| None` | no `type`, no `items` | `type: [array, null]`, 
`items: {type: string}` |
   | `options: Dict[str, int] \| None` | no `type` | `type: [object, null]` |
   | `place: Location \| None` (a model) | no `type` | unchanged |
   | `key: int \| str \| None` | no `type` | unchanged |
   
   #### Behavioral contracts
   
   1. A property `anyOf: [S, {"type": "null"}]`, where `S` has a single string 
`type`, is sent as `S` merged with the property's own keys, with `type: [<type 
of S>, "null"]`. The SDK then keeps only `type`, `items`, `description` and 
`enum`.
   2. If `S` has an `enum` that does not contain `None`, `None` is appended to 
it.
   3. Every other property is sent as before.
   4. The tool name, description and `required` list are sent as before.
   5. The call does not change the tool's metadata.
   
   #### Failure behavior
   
   The rewrite does not raise. A schema it does not recognize is sent as 
before. Only the `tools` of Ollama requests change. Other connections, and the 
validation of tool call arguments, are not touched.
   
   ### Tests
   
   The new `test_ollama_tool_schema.py` holds unit tests (not marked 
`integration`). The first two send a chat request through a real 
`ollama.Client` whose `httpx` transport is mocked, and the first checks the 
request body that the SDK serializes. The third checks the payload given to the 
SDK, because the SDK would drop an `anyOf` either way.
   
   | Contract | Test |
   |---|---|
   | 1, 2, 4 | 
`test_nullable_parameters_keep_their_type_after_sdk_serialization` |
   | 3 | `city: str` in the same test, and 
`test_other_schemas_are_left_unchanged` |
   | 5 | `test_tool_metadata_is_unchanged` |
   
   With the call site changed back to `to_openai_tool`, the first test fails: 
the nullable parameters reach the request body with only a description. If the 
rewrite is widened to a union of several types, the third test fails.
   
   Not verified:
   
   - Not run against a live Ollama server. The `enum` with `null`, the array 
and the dict cases were not tried against a model.
   - Python 3.10 and 3.11 were not run.
   
   <details>
   <summary>Test runs</summary>
   
   - `pytest 
flink_agents/integrations/chat_models/tests/test_ollama_tool_schema.py`: 3 
passed.
   - The unit selection of `tools/ut.sh -p` on Python 3.12: 3490 passed, 18 
skipped, 20 failed. The 20 are in `test_output_type_utils.py` and 
`test_remote_execution_environment.py`, and fail with `No such file or 
directory: 'java'` because no JVM is installed in the local environment.
   - `ruff check --no-fix .` passes, and `ruff format --check` passes on the 
two changed files.
   
   </details>
   
   ### API
   
   No public API change. Only the `tools` payload of Ollama requests changes: 
models are now told that these parameters accept `null`, which argument 
validation already accepts.
   
   ### Documentation
   
   - [ ] `doc-needed`
   - [x] `doc-not-needed`
   - [ ] `doc-included`
   
   The providers page notes the Java limitation for nullable parameters. The 
Python connection now has no such limitation, so no Python note is added.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   - [x] Yes
   - [ ] No
   
   Generated-by: Claude Code 2.1.308 (Claude Opus 5.5)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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