Skip to main content
SGLang provides comprehensive support for function calling (tool calling), enabling models to interact with external tools and APIs. This follows the OpenAI function calling specification.

Supported Parsers

Quick Start

Launch Server

The --tool-call-parser argument specifies which parser to use for interpreting function calls in the model’s output.

Define Tools

Make Requests

Non-Streaming

Streaming

Multi-Tool Support

Define multiple tools for the model to choose from:

Tool Choice Control

Control when and how the model calls tools:

Multi-Turn Conversations

Implement agentic workflows with tool execution:

Parallel Tool Calls

Some models support calling multiple tools in one turn:

Pythonic Tool Calling

Some models can output function calls as executable Python code:
The model generates:
Instead of JSON format.

Model-Specific Notes

DeepSeek-V3 family supports thinking before tool calls:
GPT-OSS uses analysis channels. The parser filters these out, but content may be empty if all output is in analysis channel. Complete the tool round by returning tool results to get final content.
For Kimi K2 with thinking, use both parsers:

Implementation Details

Tool calling is implemented through the FunctionCallParser system:
Source: python/sglang/srt/function_call/function_call_parser.py:39 Each detector implements:
  • Pattern detection: Identify tool call syntax in output
  • Argument extraction: Parse JSON/Python arguments
  • Streaming support: Handle incremental parsing
  • Validation: Ensure arguments match schema

Combining with Structured Outputs

You can combine tool calling with structured outputs for precise control:

Best Practices

Write descriptive tool names and clear parameter descriptions. This helps the model understand when and how to use each tool.
Mark essential parameters as required in the schema. This ensures the model provides all necessary information.
Always validate tool call arguments before execution. Handle parsing errors and missing parameters appropriately.
Set reasonable timeouts for tool execution to prevent hanging on slow APIs.
Define enum fields for parameters with fixed options (e.g., units, categories).

Performance Considerations

  • Parser overhead: Minimal (<1ms per request)
  • Streaming latency: Tool calls appear incrementally in stream
  • Multi-tool calls: Some parsers support multiple calls per turn
  • Validation: Schema validation adds negligible overhead

Limitations

  • Parser support varies by model architecture
  • Some models may hallucinate tool calls not in the provided list
  • Complex nested schemas may confuse some models
  • Streaming with parallel tool calls may have delayed final chunks