Skip to content

Fix LFM2 tool calling with nested parentheses in arguments#152

Merged
davidkoski merged 10 commits intoml-explore:mainfrom
tpae:main
Mar 20, 2026
Merged

Fix LFM2 tool calling with nested parentheses in arguments#152
davidkoski merged 10 commits intoml-explore:mainfrom
tpae:main

Conversation

@tpae
Copy link
Copy Markdown
Contributor

@tpae tpae commented Mar 18, 2026

Summary

  • Fix PythonicToolCallParser regex that truncated tool call arguments containing nested parentheses (e.g., requests.get('url') inside a code= argument). The root cause was optional brackets \[?...\]? in the regex -- the Python reference uses required brackets \[...\], which forces the non-greedy .*? to backtrack past inner ) characters. A string-based fallback handles the without-brackets case.
  • Add "*.jinja" to downloadModel file patterns so chat_template.jinja is cached alongside model weights for offline robustness.

Context

LFM2-24B-A2B (LiquidAI/LFM2-24B-A2B) uses Pythonic tool call syntax wrapped in <|tool_call_start|> / <|tool_call_end|> tags. When argument values contain nested parentheses (common with code-execution tools like sandbox_run_script), the regex matched the first ) inside the value rather than the correct outer one, producing mangled arguments.

Reference: mlx-lm/tool_parsers/pythonic.py

Test plan

  • New test: nested parentheses with brackets ([run_script(code="requests.get('url')")])
  • New test: nested parentheses without brackets (run_script(code="print('hello')"))
  • All 37 existing ToolTests pass (no regressions)

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

@tpae
Copy link
Copy Markdown
Contributor Author

tpae commented Mar 20, 2026

@davidkoski any chance this could be merged?

Copy link
Copy Markdown
Collaborator

@davidkoski davidkoski left a comment

Choose a reason for hiding this comment

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

Looks good. I wonder if this will be tripped up with batch tokens where it might have parens in trailing text?

@davidkoski davidkoski merged commit edd42fc into ml-explore:main Mar 20, 2026
2 checks passed
@davidkoski davidkoski mentioned this pull request Mar 27, 2026
4 tasks
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.

2 participants