Skip to content

Fix intermittent empty/truncated Grok search responses#34

Open
dztabel-happy wants to merge 1 commit intoGuDaStudio:mainfrom
dztabel-happy:codex/fix-grok-empty-content
Open

Fix intermittent empty/truncated Grok search responses#34
dztabel-happy wants to merge 1 commit intoGuDaStudio:mainfrom
dztabel-happy:codex/fix-grok-empty-content

Conversation

@dztabel-happy
Copy link

Summary

This fixes an intermittent issue where web_search can return an empty or truncated content string even though the upstream request succeeds and sources_count is non-zero.

Reproduction

Using the current main implementation against a real Grok-compatible endpoint, repeated calls to GrokSearchProvider.search(...) intermittently returned incomplete results:

  • empty string responses
  • very short partial prefixes like "**" or "**Scrape.do** is a"
  • normal full responses on other runs

In one 6-run sample before this patch, 2 runs returned an empty string and several others were truncated.

Root Cause

The provider currently depends on streaming chat completions (stream=True) and concatenates SSE deltas.
For this upstream, streaming responses are not consistently delivered as a complete answer, which causes partial or empty content to propagate into web_search.

Fix

  • switch Grok provider search/fetch/URL description/source ranking calls to non-stream completions (stream=False)
  • add explicit Accept and User-Agent headers for the upstream API request
  • parse standard JSON chat completion bodies first
  • keep an SSE-text fallback parser for compatibility if an upstream still responds with data: lines

Validation

  • Added regression tests for:
    • non-stream payload + request headers
    • JSON completion parsing
    • SSE-text fallback parsing
  • Ran uv run --extra dev pytest tests/test_grok_provider.py -q
  • Re-ran the same 6-run live search loop after the patch: EMPTY_COUNT dropped from 2 to 0
  • Verified end-to-end MCP web_search calls via local stdio server returned non-empty content across repeated runs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant