Skip to content

Ollama integration tests don't catch context-window truncation causing empty responses #1443

Closed

Description

@malibio

Overview

Three bugs caused Ollama to silently truncate the system prompt at 4096 tokens, producing a 1-token empty response with no tool calls. None of the existing integration tests caught this because test_ollama_inference_with_tools explicitly treats an empty response as a valid outcome.

Problem Statement

What broke: Sending a chat message in the app produced no response and logged Agent returned empty response with no tool calls with prompt_tokens=4095 and completion_tokens=1.

Root cause — three compounding bugs in packages/agent/src/local_agent/ollama_inference.rs:

  1. num_ctx is never included in OllamaOptions — Ollama defaults to 4096 regardless of the model's actual context window. The production system prompt is ~1,900 tokens and 14 tool definitions push total prompt tokens to ~4,095, hitting this hard limit.

  2. OllamaShowResponse deserializes field modelinfo (camelCase) but the Ollama /api/show response uses model_info (snake_case) — serde always produces None, context detection always falls through to the 4096 fallback.

  3. Key mismatch: even if serde were correct, the code looks for llm.context_length but Gemma 4 reports gemma4.context_length. The key is model-family-specific.

Why tests didn't catch it:

test_ollama_inference_with_tools (line 186, packages/agent/tests/ollama_integration.rs) explicitly accepts empty responses:

// Model may or may not call the tool — both are valid responses.
// Not all models support tool calling; some return no chunks in that case.
// Verify only that no Error chunks were emitted.

The test uses a tiny 2-message prompt (system + user) well under 4096 tokens, so truncation doesn't happen in that test. And even if it did, the assertion only checks for StreamingChunk::Error chunks — an empty response passes silently.

There is no test that exercises LocalAgentService::send_message() with a realistic production prompt (full system prompt + 14 tools + chat history) against a real Ollama, which is exactly the scenario that broke.

Proposed Solution

Fix the three bugs

  • Add num_ctx: Option<u32> to OllamaOptions; set it to the model's context window (from /api/show) when tools are present, defaulting to 32768 if detection fails
  • Fix OllamaShowResponse field name: rename modelinfo → model_info (add #[serde(rename = "model_info")] or use the snake_case field name directly)
  • Fix the context key lookup: try model-family-specific keys (gemma4.context_length, llama.context_length, etc.) before falling back; or iterate all keys looking for one ending in .context_length

Strengthen the integration tests

  • test_ollama_inference_with_tools: assert the response is non-empty — either at least one ToolCallStart chunk OR at least one non-empty Token chunk. An empty response is an inference failure, not a valid outcome for a tool-capable model.
  • Add test_ollama_inference_with_production_prompt: use a realistic system prompt (~7KB) and all 14 production tools against LocalAgentService::send_message(), assert a non-empty response. This is the scenario that was broken in production.
  • Add test_ollama_model_info_context_window_detection: assert model_info() returns a context_window > 4096 for the available model (Gemma 4 has 131072). This pins the serde field name and key lookup correctness.

Acceptance Criteria

  • num_ctx is set on all Ollama requests where tools are present
  • OllamaShowResponse correctly deserializes model_info from the Ollama API response
  • Context window detection correctly reads gemma4.context_length (and equivalent keys for other model families)
  • test_ollama_inference_with_tools asserts a non-empty response (tool call or text)
  • New test test_ollama_inference_with_production_prompt passes against gemma4:e4b with a full system prompt and 14 tools
  • New test test_ollama_model_info_context_window_detection asserts context_window > 4096
  • All tests skip gracefully when Ollama is not running
  • bun run test:all passes with no new failures
  • Code passes bun run quality:fix

Technical Specifications

Files to Modify

  • ollama_inference.rs: packages/agent/src/local_agent/ollama_inference.rs

    • OllamaOptions struct (line 71): add num_ctx: Option<u32>
    • OllamaShowResponse struct (line 107): fix field name modelinfo → model_info
    • model_info() method (line 304): fix context key lookup chain
    • generate() method (line 113): populate num_ctx from model info or safe default
  • ollama_integration.rs: packages/agent/tests/ollama_integration.rs

    • test_ollama_inference_with_tools (line 186): strengthen assertion
    • Add test_ollama_inference_with_production_prompt
    • Add test_ollama_model_info_context_window_detection

Context Window Detection Fix

Suggested key lookup order for model_info():

let context_window = ["llm.context_length", "gemma4.context_length", "llama.context_length", "qwen2.context_length"]
    .iter()
    .find_map(|key| modelinfo.get(*key).and_then(|v| v.as_u64()))
    .unwrap_or(32_768) as u32;

Or more robustly: iterate modelinfo keys and return the first value where the key ends with .context_length.

Safe Default

If /api/show fails or the key is not found, default num_ctx to 32768 (not 4096). The production system prompt + 14 tools fits within 32K for any current model.

Activity

  1. malibio commented on Jun 22, 2026

    @malibio
    CollaboratorAuthor

    Frontend: 140 passed (baseline). Rust unit: 248 passed. test:all: 957 passed. Implementing fixes now.

  2. self-assigned this
    on Jun 22, 2026
  3. added 2 commits that reference this issue on Jun 22, 2026
    ead7dc4
    13f92c8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions