Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Ollama Six Major Model Capabilities in Practice

Ollama Six Major Model Capabilities in Practice

Streaming output, thinking mode, structured output, visual understanding, vector embeddings, tool calling, plus web search, make up the complete landscape of Ollama model capabilities.

This chapter practices each capability one by one with the official Python library; all examples can be run directly.

First, we need to install the Ollama Python SDK.

It can be installed with pip:



pip install ollama

Make sure Python 3.x is installed in your environment and that the network can access the local Ollama service.

Before using the Python SDK, make sure the local Ollama service has been started.

You can use the command-line tool to start it:



ollama serve

After starting the local service, the Python SDK communicates with it to perform tasks such as model inference.


Capability 1: Streaming output

Streaming makes answers appear word by word on the interface and is the foundation of the chat application experience.

In the SDK, set stream to True, then iterate over each data chunk:

Example

from

ollama

import

chat

Enable streaming and receive the response chunk by chunk

stream

=

chat

(

    model

=

'qwen3.5'

,

    messages

=

[

{

'role'

:

'user'

,

'content'

:

'Introduce RUNOOB Runoob Tutorial in three sentences'

}

]

,

    stream

=

True

,

)

Print chunk by chunk while concatenating the full content

content

=

''

for

chunk

in

stream:

   

print

(

chunk

.

message

.

content

,

end

=

''

,

flush

=

True

)

    content +

=

chunk

.

message

.

content

The concatenated content can be used to save history and archive to the database

Key point: each streaming data chunk is just a "fragment"; you must accumulate the full content yourself on the client. When later adding this turn's answer to the conversation history, use the concatenated full text.


Capability 2: Thinking mode

Reasoning models first output a thinking process before giving the answer; in the SDK this is controlled by the think parameter, with thinking and the main text in two separate fields.

Example

from

ollama

import

chat

response

=

chat

(

    model

=

'qwen3.5'

,

    messages

=

[

{

'role'

:

'user'

,

'content'

:

'Which is larger, 9.9 or 9.11?'

}

]

,

    think

=

True

,

    stream

=

False

,

)

The thinking process and the final answer are in two separate fields

print

(

'Thinking:'

,

response.

message

.

thinking

)

print

(

'Answer:'

,

response.

message

.

content

)

In streaming scenarios, thinking and content appear alternately in data chunks; switch the rendering area using a state machine:

Example

from

ollama

import

chat

stream

=

chat

(

    model

=

'qwen3.5'

,

    messages

=

[

{

'role'

:

'user'

,

'content'

:

'What is 17 times 23?'

}

]

,

    think

=

True

,

    stream

=

True

,

)

in_thinking

=

False

for

chunk

in

stream:

   

if

chunk

.

message

.

thinking

:

       

if

not

in_thinking:

            in_thinking

=

True

           

print

(

'[Thinking]'

,

end

=

''

,

flush

=

True

)

       

print

(

chunk

.

message

.

thinking

,

end

=

''

,

flush

=

True

)

   

elif

chunk

.

message

.

content

:

       

if

in_thinking:

            in_thinking

=

False

           

print

(

'\n[Answer]'

,

end

=

''

,

flush

=

True

)

       

print

(

chunk

.

message

.

content

,

end

=

''

,

flush

=

True

)

UI implementation suggestion: render thinking as a collapsible gray area and content as the main text; the gpt-oss series only accepts three thinking-effort levels: low / medium / high; passing a boolean has no effect.


Capability 3: Structured output

Structured output makes the model return data that strictly conforms to a JSON Schema; combined with Pydantic validation, it is the standard way for programs to consume model output.

Example

from

ollama

import

chat

from

pydantic

import

BaseModel

Define the expected data structure with Pydantic

class

Site

(

BaseModel

)

:

    name:

str

    category:

str

    free:

bool

response

=

chat

(

    model

=

'qwen3.5'

,

    messages

=

[

{

'role'

:

'user'

,

'content'

:

'Introduce RUNOOB Runoob Tutorial'

}

]

,

    format

=

Site.

model_json_schema

(

)

,

)

The returned content is a JSON string conforming to the schema, ready to validate and parse directly

site

=

Site.

model_validate_json

(

response.

message

.

content

)

print

(

site

.

name

,

site

.

category

,

site

.

free

)

This pattern is the cornerstone of all data-extraction applications: extracting fields from resumes, elements from tickets, information from images (combined with visual capability) all use it.

Two stability tips: set temperature in options to 0; also describe the meaning of each field in the prompt, double-constraining it with the schema.


Capability 4: Visual understanding

Multimodal models (such as qwen3.5) can accept images, and the SDK supports passing file paths directly, much more convenient than the base64 encoding of the REST API.

Example

from

ollama

import

chat

response

=

chat

(

    model

=

'qwen3.5'

,

    messages

=

[

{

       

'role'

:

'user'

,

       

'content'

:

'What page is this screenshot? List the main functions.'

,

       

'images'

:

[

'screenshot.png'

]

,

   

}

]

,

)

print

(

response.

message

.

content

)

Combining vision with structured output enables "image-to-data":

Example

from

ollama

import

chat

from

pydantic

import

BaseModel

Define the target structure to extract from the image

class

Receipt

(

BaseModel

)

:

    merchant:

str

    total:

float

    date:

str

response

=

chat

(

    model

=

'qwen3.5'

,

    messages

=

[

{

       

'role'

:

'user'

,

       

'content'

:

'Extract the merchant, amount, and date from this receipt photo'

,

       

'images'

:

[

'receipt.jpg'

]

,

   

}

]

,

    format

=

Receipt.

model_json_schema

(

)

,

    options

=

{

'temperature'

:

0

}

,

)

print

(

Receipt.

model_validate_json

(

response.

message

.

content

)

)


Capability 5: Vector embeddings

The embed interface produces text vectors in batches; combined with cosine similarity, a minimal viable semantic search can be implemented.

Example

import

ollama

Generate document vectors in batches

docs

=

[

   

'Python is an interpreted language'

,

   

'JavaScript mainly runs in the browser'

,

   

'RUNOOB provides free programming tutorials'

,

]

result

=

ollama.

embed

(

model

=

'embeddinggemma'

,

input

=

docs

)

vectors

=

result

[

'embeddings'

]

print

(

len

(

vectors

)

,

len

(

vectors

[

0

]

)

)

 

3 vectors and their dimensions

Compute cosine similarity between the query vector and document vectors to rank and retrieve

query

=

ollama.

embed

(

model

=

'embeddinggemma'

,

input

=

'Is it hard to learn Python?'

)

Indexing and querying must use the same embedding model, otherwise the vector spaces are inconsistent and retrieval results are meaningless. The complete knowledge-base practice is covered in the RAG project chapter.


Capability 6: Tool calling

Tool calling teaches the model to "ask for help": when it determines that external information is needed, it returns tool_calls; the program executes the actual function, and after the result is returned, the model summarizes and answers.

First look at the complete Agent loop sequence to understand how messages flow among the four parties:

Tool-calling Agent loop sequence diagram

The Python SDK allows passing functions directly as tools; the function signature and docstring are automatically parsed into a schema:

Example

from

ollama

import

chat

Local tool function: the docstring becomes the tool description seen by the model

def

get_weather

(

city:

str

)

str

:

   

"""Query the current temperature of a specified city

    Args:
        city: city name

    Returns:
        Current temperature description
    """

    temperatures

=

{

"Beijing"

:

"22°C"

,

"Shanghai"

:

"26°C"

,

"New York"

:

"18°C"

}

   

return

temperatures.

get

(

city

,

"Unknown city"

)

messages

=

[

{

'role'

:

'user'

,

'content'

:

'How hot is it in New York now?'

}

]

Agent loop: repeat until the model no longer requests tools

while

True

:

    response

=

chat

(

        model

=

'qwen3.5'

,

        messages

=

messages

,

        tools

=

[

get_weather

]

,

   

)

   

Append the model message (including tool_calls) to the history

    messages.

append

(

response.

message

)

   

if

not

response.

message

.

tool_calls

:

       

No more tool requests; output the final answer

       

print

(

response.

message

.

content

)

       

break

   

Tool requests present: execute and return the results with the tool role

   

for

call

in

response.

message

.

tool_calls

:

        result

=

get_weather

(

**call.

function

.

arguments

)

        messages.

append

(

{

           

'role'

:

'tool'

,

           

'tool_name'

: call.

function

.

name

,

           

'content'

: result

,

       

}

)

Three engineering points: first, each response.message must be appended back to messages verbatim, and tool results returned with the tool role; second, with parallel tool calls the model may return multiple tool_calls at once, so execute and append them one by one; third, in real projects tool results can be very long, so mind truncation to protect the context window.


Capability 7: Web search

Ollama officially provides two web interfaces, web_search and web_fetch, which can be attached to the model as tools so it can answer about new information beyond its training data.

Example

from

ollama

import

chat

,

web_search

,

web_fetch

Attach web capability as a tool

available_tools

=

{

'web_search'

: web_search

,

'web_fetch'

: web_fetch

}

messages

=

[

{

'role'

:

'user'

,

'content'

:

"What new features has Ollama added recently?"

}

]

while

True

:

    response

=

chat

(

        model

=

'qwen3.5'

,

        messages

=

messages

,

        tools

=

[

web_search

,

web_fetch

]

,

        think

=

True

,

   

)

    messages.

append

(

response.

message

)

   

if

not

response.

message

.

tool_calls

:

       

print

(

response.

message

.

content

)

       

break

   

for

call

in

response.

message

.

tool_calls

:

        func

=

available_tools.

get

(

call.

function

.

name

)

       

if

func:

           

Tool results can be very long; truncate to protect context

            result

=

str

(

func

(

**call.

function

.

arguments

)

)

            messages.

append

(

{

               

'role'

:

'tool'

,

               

'tool_name'

: call.

function

.

name

,

               

'content'

: result

[

:

2000

]

,

           

}

)

Web interfaces require creating an API Key at ollama.com; search results often run to thousands of tokens, and the official recommendation is to set the context to above 32K for such Agent scenarios. It also has ready-made MCP Server implementations that can integrate with tools like Cline and Codex; see the ecosystem chapter for integration details.