Skip to content

Preserve nullable string arguments in glm47 tool parser - #2085

Open
KarmanyaIyer wants to merge 1 commit into
ml-explore:mainfrom
KarmanyaIyer:contrib/glm47-nullable-string
Open

KarmanyaIyer wants to merge 1 commit into
ml-explore:mainfrom
KarmanyaIyer:contrib/glm47-nullable-string

Conversation

@KarmanyaIyer

Copy link
Copy Markdown

Problem

mlx_lm/tool_parsers/glm47.py (used by GLM-4.5, 4.6, 4.7, 4.7-Flash, and Poolside Laguna via laguna.py) only keeps an argument as a string when its schema has "type": "string".

Nullable string schemas are not recognized:

  • Pydantic v2 Optional[str]: {"anyOf": [{"type": "string"}, {"type": "null"}]}
  • Multi-type JSON Schema: {"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 to json.loads and ast.literal_eval, coercing "12345" to 12345, "1.10" to 1.1, "true" to True, and '{"a": 1}' to a dict. Clients validating tool calls with Pydantic Optional[str] reject these values.

Changes

  • Added _schema_types() to read types from "type" (string or list) and "anyOf". Non-dict schemas or schemas without a type (such as $ref or {}) are not treated as strings.
  • Updated _get_string_arg_names() to match parameters where types - {"null"} == {"string"} (leaving mixed unions like ["string", "integer"] unchanged) and return {name: accepts_null}.
  • Added _parse_arg_value(), shared by the XML and key=value parsing paths:
    • Nullable string parameters parse "null" as None (matching how the chat template renders None).
    • Plain "type": "string" parameters keep "null" as the string "null".
  • Updated _normalize_arguments() so None stays None for 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_args in tests/test_tool_parsing.py covering both anyOf and ["string", "null"] schemas, string preservation ("12345", "true"), and null to None conversion in both XML and JSON forms.

  • Ran tests.test_tool_parsing and tests.test_server (64 tests pass) and pre-commit (black, isort, ruff).

  • Verified end-to-end against mlx_lm.server with mlx-community/Laguna-XS.2-2bit-M-DQ (temperature 0) using a Pydantic zip_code: Optional[str] tool schema:

    • main: returns {"zip_code": 12345} (fails Pydantic validation with string_type error)
    • branch: returns {"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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant