fix: validate_tool_arguments does not enforce Literal/const constraints on tool fields - #1547
Open
JessicaHe22 wants to merge 2 commits into
Open
fix: validate_tool_arguments does not enforce Literal/const constraints on tool fields#1547JessicaHe22 wants to merge 2 commits into
JessicaHe22 wants to merge 2 commits into
Conversation
AngeloDanducci
requested changes
Aug 24, 2026
AngeloDanducci
left a comment
Contributor
There was a problem hiding this comment.
Hi Jessica, thanks for the PR!
Overall I think this looks pretty good. Are single value constraints handled as well? I have a suggestion for an additional test to verify.
You will want to install the pre-commit hooks to use in the future (I see you fixed a ruff format problem). Additionally you need to sign off on your commits. To fix this you can do the following:
To add your Signed-off-by line to every commit in this branch:
Ensure you have a local copy of your branch by [checking out the pull request locally via command line](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/checking-out-pull-requests-locally).
In your local branch, run: git rebase HEAD~2 --signoff
Force push your changes to overwrite the branch: git push --force-with-lease origin jessicah/issue-1106
In the future when you commit if you use an -s flag in your git commit commands ie git commit -s -m "commit message" you won't have to do the above. If you have any issues with this let me know and we can work around it.
| result = validate_tool_arguments( | ||
| mt, {"path": "/tmp/file.txt", "mode": "read"}, strict=True | ||
| ) | ||
| assert result["mode"] == "read" |
Contributor
There was a problem hiding this comment.
I think it's worth adding a test for single value constraints.
Suggested change
| assert result["mode"] == "read" | |
| assert result["mode"] == "read" | |
| def test_strict_rejects_value_outside_single_value_literal(self): | |
| """A single-value Literal[...] (emitted as const) must also be enforced.""" | |
| from typing import Literal | |
| from pydantic import ValidationError | |
| def tag_op(name: str, kind: Literal["cat"]) -> str: | |
| """Tag something. | |
| Args: | |
| name: the name | |
| kind: the kind | |
| """ | |
| return "ok" | |
| mt = MelleaTool.from_callable(tag_op) | |
| # The allowed value must survive into the schema the backend sees. | |
| props = mt.as_json_tool["function"]["parameters"]["properties"] | |
| assert props["kind"].get("enum") == ["cat"] | |
| with pytest.raises(ValidationError): | |
| validate_tool_arguments( | |
| mt, {"name": "Bob", "kind": "horse"}, strict=True | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request
Issue
Fixes #1106
Description
The issue #1106 identified two independent gaps where
Literal[...]constraints on tool fields were silently dropped.Gap A — validator ignores
conston discriminated union tag fieldsAlready fixed in previous PR. This PR adds a test (
test_strict_rejects_invalid_discriminator_value) to explicitly assert that a discriminator value matching no branch (e.g.kind: "horse"when only"cat"and"dog"are defined) is rejected understrict=True.Gap B —
enumkeyword dropped for plainLiteral[...]fieldsFixed in this PR with two changes:
convert_function_to_ollama_tool: the simple-type flattening branch was discarding theenumarray that Pydantic emits forLiteralfields. It now carriesenumforward onto the output schema so the LLM sees the allowed values._build_pydantic_type_from_schema: the simple-type fallback was mapping anytype: stringfield to plainstr, ignoringenumandconst. It now mapsenum→Literal[V1, V2, ...]andconst→Literal[V]before falling through to the bare type lookup, so Pydantic enforces the constraint during validation.Testing
Attribution
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.
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.