Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Ollama Ollama Tools Parser

From Leeroopedia
Knowledge Sources
Domains Tool Calling, Streaming Parser
Last Updated 2025-02-15 00:00 GMT

Overview

Implements a streaming parser that detects and extracts tool calls from model output, matching function names and arguments against a list of available tools.

Description

The Parser is a three-state machine (LookingForTag, ToolCalling, Done) that processes model output incrementally via the Add method. It buffers incoming text, searches for the tool call tag (extracted from the model's chat template), then enters tool calling mode. findTool matches tool names in the buffer with disambiguation for partial names. findArguments parses JSON objects from the buffer, searching for "arguments" or "parameters" fields within nested structures. For models using { or [ as tags, tool calling is only attempted if the first non-whitespace character matches. The parser returns both tool calls and content that should be sent to the user.

Usage

Used during chat completion responses when tools are provided in the request. The parser processes each token as it is generated by the model, accumulating text until a complete tool call is detected or the generation ends.

Code Reference

Source Location

  • Repository: Ollama
  • File: tools/tools.go
  • Lines: 1-381

Signature

type Parser struct {
    // unexported fields: state, buffer, tag, tools, etc.
}

func GetBuffer(s string) string
func NewParser(tools []api.Tool, template string) *Parser
func NewParserWithTag(tools []api.Tool, tag string) *Parser
func (p *Parser) Add(s string) (string, []api.ToolCall, error)

Import

import "github.com/ollama/ollama/tools"

I/O Contract

Inputs

Name Type Required Description
tools []api.Tool Yes List of available tools with their function names and parameter schemas
template string Yes Chat template string used to extract the tool call tag
s string Yes Incremental model output text to parse (Add method)

Outputs

Name Type Description
content string Text content to pass through to the user (not part of tool calls)
toolCalls []api.ToolCall Detected tool calls with function name and arguments
error error Parsing error if tool call JSON is malformed

Usage Examples

// Create a parser with available tools
parser := tools.NewParser(requestTools, chatTemplate)

// Process model output token by token
for token := range modelOutput {
    content, toolCalls, err := parser.Add(token)
    if err != nil {
        // handle error
    }
    if content != "" {
        // send content to user
    }
    for _, tc := range toolCalls {
        // execute tool call: tc.Function.Name, tc.Function.Arguments
    }
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment