Skip to content

fix: enable tool calling for ChatMLX (fixes langchain-ai/langchain#31… #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Ujjwal-Bajpayee
Copy link

@Ujjwal-Bajpayee Ujjwal-Bajpayee commented Jun 25, 2025

What does this PR do?

Adds support for tool/function calling in ChatMLX using MLXPipeline.

Why is this needed?

Fixes a bug where .tool_calls was always empty even when a model (e.g. Phi-4-reasoning-plus-MLX-4bit) supports tool calling.
Reference: langchain-ai/langchain#31726

Changes made:

  • Injects tool schema into the prompt when tools are bound
  • Parses .tool_calls from the model’s output (if JSON structured)
  • Keeps the class structure and logic unchanged otherwise

@matiasdev30
Copy link

Hi still returning empty list [] in res.tool_calls

`from langchain_community.chat_models.mlx import ChatMLX
from langchain_community.llms import MLXPipeline
from langchain_core.tools import tool

@tool
def multiply(a: int, b: int) -> int:
"""Multiplies two integers."""
return a * b

chat = ChatMLX(
llm=MLXPipeline.from_model_id("Phi-4-reasoning-plus-MLX-4bit")
).bind_tools([multiply])

res = chat.invoke("What is 2 multiplied by 3?")
#print(res.content)
print(res.tool_calls)`

[]

@Ujjwal-Bajpayee
Copy link
Author

@matiasdev30 Please check this and let me know

@Ujjwal-Bajpayee
Copy link
Author

@matiasdev30

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