Basic Function Calling
The fundamental pattern for function calling with Qwen models.Complete Example
function_calling.py
How It Works
Define Functions
Create Python functions with clear docstrings and parameter definitions in JSON Schema format
Function Definition Schema
Follow this pattern to define functions:Basic Structure
Parameter Types
- String
- Number
- Boolean
- Object
- Array
Parallel Function Calling
Execute multiple functions simultaneously for better efficiency.Example
function_calling_in_parallel.py
Benefits of Parallel Calling
Better Performance
Execute multiple independent functions simultaneously
Natural Queries
Handle queries asking about multiple items
Reduced Latency
One LLM call instead of multiple sequential calls
More Efficient
Fewer API requests overall
Advanced Configuration
Function Choice Control
Control when and which functions are called:Prompt Types
Qwen-Agent supports different function calling prompt formats:- Qwen (Default)
- Nous
Optimized for Qwen models:Best for: Qwen2.5, Qwen3, QwQ
Token Management
Vision Model Function Calling
Use functions with vision models:Error Handling
Best Practices
Retry Configuration
Using with Different Model Providers
DashScope
DashScope OpenAI-Compatible Mode
Together.AI
Self-Hosted (vLLM)
Common Patterns
Weather Service
Database Query
File Operations
Troubleshooting
Function not being called
Function not being called
- Ensure function description is clear and relevant
- Check parameter descriptions are detailed
- Verify function name doesn’t conflict
- Try adding examples in the description
Invalid arguments passed
Invalid arguments passed
- Use json5 library for lenient parsing
- Add validation in function implementation
- Provide clear parameter descriptions
- Use enum for restricted values
Parallel calls not working
Parallel calls not working
- Verify
parallel_function_calls=Trueis set - Check model supports parallel calls
- Ensure functions are independent
Function result not processed
Function result not processed
- Verify function returns valid JSON string
- Check role is set to ‘function’ in message
- Ensure function name matches exactly
Next Steps
Assistant Demos
See function calling in complete assistants
Multi-Agent Chat
Combine function calling with multiple agents