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
Copy Markdown

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

konbakuyomu pushed a commit to konbakuyomu/GrokSearch that referenced this pull request Mar 30, 2026
- Grok provider 从流式切换为非流式 completion (stream=False)
- 新增 _build_api_headers() 统一 headers (Accept/User-Agent)
- 新增 _parse_completion_response() JSON 优先 + SSE fallback 解析
- 新增 _execute_completion_with_retry() 非流式重试执行器
- 新增 _extract_request_id()/_extract_error_summary() 错误处理辅助
- 修复 sanitize_answer_text() 对 None 输入的 TypeError
- 移除 logger.py 未使用的 Path 导入
- 添加 35 项回归测试覆盖 provider/sources/planning

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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