Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Principle:Ollama Ollama OpenAI Response Translation

From Leeroopedia
Knowledge Sources
Domains API_Design, Data_Transformation
Last Updated 2026-02-14 00:00 GMT

Overview

A response format translation mechanism that converts Ollama's native streaming inference responses into OpenAI-compatible chat completion and completion response formats.

Description

OpenAI Response Translation converts Ollama's internal response format (api.ChatResponse, api.GenerateResponse) into OpenAI's response format (ChatCompletion, ChatCompletionChunk, Completion). This includes mapping fields, computing usage statistics, setting finish reasons, and formatting streaming SSE (Server-Sent Events) chunks.

The translation handles both streaming mode (producing data: {json}\n\n SSE events) and non-streaming mode (producing a single JSON response with all content aggregated).

Usage

Use this principle when implementing the response side of an API compatibility layer. The translator must handle both streaming and non-streaming modes and correctly compute usage statistics from the native response metrics.

Theoretical Basis

Response translation maps:

Ollama Field OpenAI Field Notes
Message.Content Choices[0].Message.Content Chat response content
Message.ToolCalls Choices[0].Message.ToolCalls Function call results
Done Choices[0].FinishReason "stop" when done, "tool_calls" if tools present
PromptEvalCount Usage.PromptTokens Input token count
EvalCount Usage.CompletionTokens Output token count
Model Model Model name

For streaming:

  • Each token chunk becomes a ChatCompletionChunk with delta content
  • The final chunk has finish_reason set
  • Usage statistics appear only in the final chunk

Related Pages

Implemented By

Page Connections

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