Implementation:Mistralai Client python Function Dispatch Pattern
| Knowledge Sources | |
|---|---|
| Domains | Function_Calling, Design_Pattern |
| Last Updated | 2026-02-15 14:00 GMT |
Overview
Interface specification for user-defined function dispatch in Mistral AI function calling workflows.
Description
This is a Pattern Doc — it documents the user-implemented dispatch pattern rather than a library API. After the model requests tool calls, the user must implement logic to: (1) look up the function by name, (2) parse the JSON arguments, (3) execute the function, and (4) collect the string result for submission back to the model. The Mistral examples use a dictionary-based dispatch pattern.
Usage
Implement this pattern when using function calling. Create a dictionary mapping function names to callables, then iterate over tool_calls from the model response to execute each one.
Code Reference
Source Location
- Repository: client-python
- File: N/A (user code pattern)
- Reference: examples/mistral/chat/function_calling.py
Interface Specification
import json
from typing import Dict, Callable, Any
# Type signature for the dispatch pattern
FunctionRegistry = Dict[str, Callable[..., Any]]
def dispatch_tool_calls(
tool_calls: list,
registry: FunctionRegistry,
) -> list:
"""
Execute tool calls and collect results.
Args:
tool_calls: List of ToolCall objects from model response
registry: Mapping of function names to callables
Returns:
List of (tool_call_id, result_string) tuples
"""
results = []
for tool_call in tool_calls:
fn = registry[tool_call.function.name]
args = json.loads(tool_call.function.arguments)
result = fn(**args)
results.append((tool_call.id, str(result)))
return results
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| tool_calls | List[ToolCall] | Yes | Tool calls from the model response |
| registry | Dict[str, Callable] | Yes | Mapping of function names to implementations |
Outputs
| Name | Type | Description |
|---|---|---|
| results | List[Tuple[str, str]] | List of (tool_call_id, result_string) for ToolMessage creation |
Usage Examples
Dictionary-Based Dispatch
import json
# Define actual functions
def get_payment_status(transaction_id: str) -> str:
# Look up payment in database
data = {"T001": "Paid", "T002": "Pending"}
return json.dumps({"status": data.get(transaction_id, "Unknown")})
def get_payment_date(transaction_id: str) -> str:
data = {"T001": "2024-01-15", "T002": "2024-02-01"}
return json.dumps({"date": data.get(transaction_id, "Unknown")})
# Build registry
registry = {
"get_payment_status": get_payment_status,
"get_payment_date": get_payment_date,
}
# Dispatch tool calls
tool_results = []
for tool_call in response.choices[0].message.tool_calls:
fn = registry[tool_call.function.name]
args = json.loads(tool_call.function.arguments)
result = fn(**args)
tool_results.append((tool_call.id, result))