Skip to content

fix(tools): carry element types and constraints into tool schemas - #1694

Merged
planetf1 merged 6 commits into
generative-computing:mainfrom
planetf1:issue-1693
Oct 9, 2026
Merged

planetf1 merged 6 commits into
generative-computing:mainfrom
planetf1:issue-1693

Conversation

@planetf1

@planetf1 planetf1 commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1693

Description

Tools taking lists, dicts or constrained values now get their arguments as declared, on every backend.

Before

  • A list[int] or list[float] argument reached the tool as strings ([1, 2] became ['1', '2']), so sum(nums) raised a TypeError inside the user's own code.
  • list[bool], list[list[int]] and lists of Pydantic models (discriminated unions included) failed validation on every call, and were passed to the tool unchecked with a warning.
  • The model was never told what goes inside a list or dict. list[int], list[Point] and dict[str, int] went out as a bare array or object, and Field constraints were dropped.
  • An explicit null for an optional parameter (limit: int | None = None) failed validation. The whole call then fell back to the raw arguments, so "count": "3" was no longer coerced to 3. (Found while working on fix(backends): parse Granite XML tool calls on the local HF path #1692.)
  • A tool schema containing a boolean sub-schema (valid JSON Schema, e.g. properties: {"a": true} from an MCP server) made validation raise even in lenient mode. On HF the generation failed; on OpenAI-compatible backends the tool call was dropped or the error escaped.

After

  • Element types arrive intact, and model or union elements are validated against their fields.
  • The model sees items, additionalProperties and constraints such as minimum, maxLength and minItems. On Ollama only items gets through, because the ollama client drops the other keywords.
  • Optional parameters accept null. A parameter with a real default (page_size: int = 10) still rejects it.
  • A schema the validator can't build falls back to the model's arguments with a warning, as lenient mode documents. Strict mode still raises.

How: convert_function_to_ollama_tool() rebuilt simple parameters from four keys and discarded the rest of Pydantic's schema. It now carries a fixed set of JSON Schema keywords (_CARRIED_SCHEMA_KEYWORDS) and flattens any discriminated unions that exposes. validate_tool_arguments() reads an items schema with no type as list[Any] rather than list[str], treats a non-required field with no non-null default as nullable, and builds its model inside the existing try.

Building the model inside try is a deliberate widening of the lenient-mode fallback, not just a fix scoped to the boolean-sub-schema case: in the default (non-strict) path, any exception raised while building the Pydantic type or constructing the validator model — not only an unbuildable boolean sub-schema — now falls back to the original arguments with a warning instead of propagating. Strict mode is unaffected and still raises.

Not covered

Testing

  • Tests added to the respective file if code was changed

  • New code has 100% coverage if code was added

  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

  • New test/backends/test_tool_collection_params_unit.py: 41 tests covering each case above through schema generation and validation. 35 of them fail on main. Validation tests use strict=True, so a pass can't come from the lenient fallback.

  • test/backends/test_tool_validation_integration.py: test_optional_param_none now runs strict, plus three new null tests (three of the four fail on main) and six boolean sub-schema tests (the three lenient ones fail on main).

  • uv run pytest test/ -m "not qualitative": 4739 passed, 21 skipped. ruff and mypy are clean.

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

The schema rebuild for simple tool parameters kept only description,
type, enum and default, dropping items, additionalProperties and Field
constraints. The model was never told a list's or dict's element type,
and validate_tool_arguments, which builds its validator from the same
schema, fell back to string elements: list[int] arguments reached the
tool as strings, and list[bool], nested lists and lists of models failed
validation and were passed through unchecked.

Copy items, additionalProperties and the standard JSON Schema
constraint keywords onto the rebuilt property, taking them from the
non-null anyOf branch for Optional parameters. In the validator, treat
an array with a missing or empty items schema as list[Any] rather than
list[str], so list[Any], tuples and external schemas without items are
no longer coerced to strings.

Fixes generative-computing#1693

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@github-actions github-actions Bot added the bug Something isn't working label Sep 29, 2026
convert_function_to_ollama_tool drops null from a simple Optional type,
so `limit: int | None = None` reaches validate_tool_arguments as a
non-required integer. The validator built that field as `int` with a
None default, so an explicit None failed: strict mode raised, and
lenient mode (used by every backend) fell back to the unvalidated
arguments, losing every other coercion in the call.

Type non-required validator fields as `T | None`. The existing
explicit-null tests passed only through the lenient fallback; they now
run with strict=True, and a new test checks the other arguments are
still coerced alongside the null.

Refs generative-computing#1693

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
Flatten discriminated unions exposed by the carried keywords. Copying
items and additionalProperties sent list[Pet], dict[str, Pet] and
list[Owner] (where a field is a discriminated union) out with raw oneOf
and discriminator, which tool APIs reject and the rest of the pipeline
strips. Re-run _recursively_flatten_in_properties after the final ref
inlining, which also covers a discriminated union in a nested model's
field.

Only treat a non-required validator field as nullable when its schema
has no non-null default. `limit: int | None = None` still accepts an
explicit None; `page_size: int = 10` rejects it again in strict mode.

Treat any items schema that names no type (annotation-only, or a
non-object form such as `true`) as list[Any], not just a missing or
empty one.

Run the correct-input validation tests with strict=True so they cannot
pass through the lenient fallback.

Refs generative-computing#1693

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
Comment-only change; no behaviour change.

Refs generative-computing#1693

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
validate_tool_arguments built its validator model outside the try, so a
schema it could not build (boolean sub-schemas such as
properties: {"a": true}, valid JSON Schema from MCP servers) raised
TypeError or AttributeError even with strict=False. The HF path does not
catch it, so generation failed; the OpenAI-compatible path dropped the
tool call as malformed on TypeError and failed on AttributeError.

Build the model inside the existing try, so lenient mode logs and
returns the original arguments and strict mode still raises. Also trim
test docstrings to one line.

Refs generative-computing#1693

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@planetf1

planetf1 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@markstur @ajbozarth @AngeloDanducci : Hi appreciate a review on this - thanks

@ajbozarth ajbozarth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback from Claude.

Traced the schema-generation and validation paths; coverage is thorough and I don't see a correctness bug. Approving. Three non-blocking notes inline — the one about null rejection on non-None defaults I'd like tracked as a follow-up issue rather than left only in the PR description.

Comment thread mellea/backends/tools.py Resolved
Comment thread mellea/backends/tools.py Resolved
Address review on generative-computing#1694: broaden the boolean-sub-schema strict test to
pytest.raises(Exception) instead of pydantic's internal failure types,
and add a dict[str, int] | None case alongside the existing optional-list
carry-through tests.

Assisted-by: Claude Code
Signed-off-by: Nigel Jones <[email protected]>
@planetf1

planetf1 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ajbozarth for the review — addressed all three notes: PR body now calls out the lenient-fallback widening explicitly, filed #1724 to track the int | None = 5 null-rejection gap, and broadened/extended the tests per your suggestions.

@planetf1
planetf1 enabled auto-merge October 8, 2026 21:26
@planetf1
planetf1 added this pull request to the merge queue Oct 8, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 8, 2026
@planetf1
planetf1 added this pull request to the merge queue Oct 9, 2026
Merged via the queue into generative-computing:main with commit 887fcde Oct 9, 2026
14 checks passed
@planetf1
planetf1 deleted the issue-1693 branch October 9, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(tools): list[int] tool arguments arrive as strings, and list/dict element types never reach the model

2 participants