Skip to content

Commit 46bb389

Browse files
Guide on using native tool calling (#8788)
* guide on native tool calling * comments
1 parent 11e9965 commit 46bb389

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
59.7 KB
Loading

docs/docs/learn/programming/tools.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ predictor = dspy.Predict(ToolSignature)
100100

101101
# Make a prediction
102102
response = predictor(
103-
question="What's the weather in New York?",
103+
question="What's the weather in New York?",
104104
tools=list(tools.values())
105105
)
106106

@@ -148,6 +148,52 @@ for call in response.outputs.tool_calls:
148148
print(f"Result: {result}")
149149
```
150150

151+
## Using Native Tool Calling
152+
153+
DSPy adapters support **native function calling**, which leverages the underlying language model's built-in tool calling capabilities rather
154+
than relying on text-based parsing. This approach can provide more reliable tool execution and better integration with models that support
155+
native function calling.
156+
157+
!!! warning "Native tool calling doesn't guarantee better quality"
158+
159+
It's possible that native tool calling produces lower quality than custom tool calling.
160+
161+
### Adapter Behavior
162+
163+
Different DSPy adapters have different defaults for native function calling:
164+
165+
- **`ChatAdapter`** - Uses `use_native_function_calling=False` by default (relies on text parsing)
166+
- **`JSONAdapter`** - Uses `use_native_function_calling=True` by default (uses native function calling)
167+
168+
You can override these defaults by explicitly setting the `use_native_function_calling` parameter when creating an adapter.
169+
170+
### Configuration
171+
172+
```python
173+
import dspy
174+
175+
# ChatAdapter with native function calling enabled
176+
chat_adapter_native = dspy.ChatAdapter(use_native_function_calling=True)
177+
178+
# JSONAdapter with native function calling disabled
179+
json_adapter_manual = dspy.JSONAdapter(use_native_function_calling=False)
180+
181+
# Configure DSPy to use the adapter
182+
dspy.configure(lm=dspy.LM(model="openai/gpt-4o"), adapter=chat_adapter_native)
183+
```
184+
185+
You can enable the [MLflow tracing](https://dspy.ai/tutorials/observability/) to check how native tool
186+
calling is being used. If you use `JSONAdapter` or `ChatAdapter` with native function calling enabled on the code snippet
187+
as provided in [the section above](tools.md#basic-setup), you should see native function calling arg `tools` is set like
188+
the screenshot below:
189+
190+
![native tool calling](../figures/native_tool_call.png)
191+
192+
193+
### Model Compatibility
194+
195+
Native function calling automatically detects model support using `litellm.supports_function_calling()`. If the model doesn't support native function calling, DSPy will fall back to manual text-based parsing even when `use_native_function_calling=True` is set.
196+
151197
## Best Practices
152198

153199
### 1. Tool Function Design

0 commit comments

Comments
 (0)