Skip to content

Commit e673935

Browse files
committed
docs: document reasoning content support for Ollama via OpenAI compatibility
Add documentation for accessing Ollama's reasoning content through the OpenAI-compatible endpoint using the reasoningContent metadata field. Fixes #3383 Signed-off-by: SenreySong <25841017+SenreySong@users.noreply.github.com> Signed-off-by: Mark Pollack <mark.pollack@broadcom.com>
1 parent 347378a commit e673935

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/ollama-chat.adoc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,50 @@ This allows you to leverage Ollama's full capabilities while using the OpenAI cl
520520

521521
image::spring-ai-ollama-over-openai.jpg[Ollama OpenAI API compatibility, 800, 600, align="center"]
522522

523+
=== Reasoning Content via OpenAI Compatibility
524+
525+
Ollama's OpenAI-compatible endpoint supports the `reasoning_content` field for thinking-capable models (such as `qwen3:*-thinking`, `deepseek-r1`, `deepseek-v3.1`).
526+
When using the Spring AI OpenAI client with Ollama, the model's reasoning process is automatically captured and made available through the response metadata.
527+
528+
NOTE: This is an alternative to using Ollama's native thinking mode API (documented in <<Thinking Mode (Reasoning)>> above).
529+
Both approaches work with Ollama's thinking models, but the OpenAI-compatible endpoint uses the `reasoning_content` field name instead of `thinking`.
530+
531+
Here's an example of accessing reasoning content from Ollama through the OpenAI client:
532+
533+
[source,java]
534+
----
535+
// Configure Spring AI OpenAI client to point to Ollama
536+
@Configuration
537+
class OllamaConfig {
538+
@Bean
539+
OpenAiChatModel ollamaChatModel() {
540+
var openAiApi = new OpenAiApi("http://localhost:11434", "ollama");
541+
return new OpenAiChatModel(openAiApi,
542+
OpenAiChatOptions.builder()
543+
.model("deepseek-r1") // or qwen3, deepseek-v3.1, etc.
544+
.build());
545+
}
546+
}
547+
548+
// Use the model with thinking-capable models
549+
ChatResponse response = chatModel.call(
550+
new Prompt("How many letter 'r' are in the word 'strawberry'?"));
551+
552+
// Access the reasoning process from metadata
553+
String reasoning = response.getResult().getMetadata().get("reasoningContent");
554+
if (reasoning != null && !reasoning.isEmpty()) {
555+
System.out.println("Model's reasoning process:");
556+
System.out.println(reasoning);
557+
}
558+
559+
// Get the final answer
560+
String answer = response.getResult().getOutput().getContent();
561+
System.out.println("Answer: " + answer);
562+
----
563+
564+
TIP: Thinking-capable models in Ollama (0.12+) automatically enable thinking mode when accessed through the OpenAI-compatible endpoint.
565+
The reasoning content is captured automatically without requiring additional configuration.
566+
523567
Check the link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/test/java/org/springframework/ai/openai/chat/proxy/OllamaWithOpenAiChatModelIT.java[OllamaWithOpenAiChatModelIT.java] tests for examples of using Ollama over Spring AI OpenAI.
524568

525569
== HuggingFace Models

0 commit comments

Comments
 (0)