Skip to content

format json disables thinking? #235

@nshiab

Description

@nshiab

Hi,

It looks like the { format : 'json' } option disables the { think: true } option.

For example, in the code below, the model thinks and returns its response as text. It works as expected. I reused the example from the documentation.

import ollama from "npm:ollama";
import process from "node:process";

async function main() {
    const response = await ollama.chat({
        model: "qwen3:8b",
        messages: [
            {
                role: "user",
                content: "What is 10 + 23",
            },
        ],
        stream: true,
        think: true,
    });

    let startedThinking = false;
    let finishedThinking = false;

    for await (const chunk of response) {
        if (chunk.message.thinking && !startedThinking) {
            startedThinking = true;
            process.stdout.write("Thinking:\n========\n\n");
        } else if (
            chunk.message.content && startedThinking && !finishedThinking
        ) {
            finishedThinking = true;
            process.stdout.write("\n\nResponse:\n========\n\n");
        }

        if (chunk.message.thinking) {
            process.stdout.write(chunk.message.thinking);
        } else if (chunk.message.content) {
            process.stdout.write(chunk.message.content);
        }
    }
}

main();

However, if we add format: 'json' to the options, then the model doesn't think anymore. There are no thoughts anymore.

import ollama from "npm:ollama";
import process from "node:process";

async function main() {
    const response = await ollama.chat({
        model: "qwen3:8b",
        messages: [
            {
                role: "user",
                content: "What is 10 + 23",
            },
        ],
        stream: true,
        think: true,
        format: "json",
    });

    let startedThinking = false;
    let finishedThinking = false;

    for await (const chunk of response) {
        if (chunk.message.thinking && !startedThinking) {
            startedThinking = true;
            process.stdout.write("Thinking:\n========\n\n");
        } else if (
            chunk.message.content && startedThinking && !finishedThinking
        ) {
            finishedThinking = true;
            process.stdout.write("\n\nResponse:\n========\n\n");
        }

        if (chunk.message.thinking) {
            process.stdout.write(chunk.message.thinking);
        } else if (chunk.message.content) {
            process.stdout.write(chunk.message.content);
        }
    }
}

main();

Does anyone have any idea what's going on?

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions