Repository navigation
Preserve nullable string arguments in glm47 tool parser - #2085
Open
KarmanyaIyer wants to merge 1 commit into
Open
KarmanyaIyer wants to merge 1 commit into
KarmanyaIyer wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Problem
mlx_lm/tool_parsers/glm47.py(used by GLM-4.5, 4.6, 4.7, 4.7-Flash, and Poolside Laguna vialaguna.py) only keeps an argument as a string when its schema has"type": "string".Nullable string schemas are not recognized:
Optional[str]:{"anyOf": [{"type": "string"}, {"type": "null"}]}{"type": ["string", "null"]}Because the GLM chat templates emit string values raw (
v | tojson if v is not string else v), the parser relies on the tool schema to skip deserialization. Unmatched nullable strings fall through tojson.loadsandast.literal_eval, coercing"12345"to12345,"1.10"to1.1,"true"toTrue, and'{"a": 1}'to a dict. Clients validating tool calls with PydanticOptional[str]reject these values.Changes
_schema_types()to read types from"type"(string or list) and"anyOf". Non-dict schemas or schemas without a type (such as$refor{}) are not treated as strings._get_string_arg_names()to match parameters wheretypes - {"null"} == {"string"}(leaving mixed unions like["string", "integer"]unchanged) and return{name: accepts_null}._parse_arg_value(), shared by the XML andkey=valueparsing paths:"null"asNone(matching how the chat template rendersNone)."type": "string"parameters keep"null"as the string"null"._normalize_arguments()soNonestaysNonefor nullable string parameters in JSON-form tool calls instead of being converted to"None".This matches vLLM's GLM parser behavior.
Testing
Added
test_glm47_nullable_string_argsintests/test_tool_parsing.pycovering bothanyOfand["string", "null"]schemas, string preservation ("12345","true"), andnulltoNoneconversion in both XML and JSON forms.Ran
tests.test_tool_parsingandtests.test_server(64 tests pass) andpre-commit(black,isort,ruff).Verified end-to-end against
mlx_lm.serverwithmlx-community/Laguna-XS.2-2bit-M-DQ(temperature 0) using a Pydanticzip_code: Optional[str]tool schema:main: returns{"zip_code": 12345}(fails Pydantic validation withstring_typeerror){"zip_code": "12345"}(passes Pydantic validation)☑️ I understand it is strictly prohibited to use AI to write PR description
AI usage disclosure:
Claude Opus 5.5 wrote the fix, the test and the repro script.
A GPT Astra 6.2 and a Claude Fable 5.2 reviewed the changes.
I prompted the fix, reviewed the diff and made edits, ran the repro, and wrote the title/description.