Skip to content

fix: sanitize OpenAI compat gateway images and surface streaming errors#2062

Open
BingqingLyu wants to merge 5 commits into
mainfrom
fork-pr-62070-fix-openai-compat-image-sanitization-and-streaming-error
Open

fix: sanitize OpenAI compat gateway images and surface streaming errors#2062
BingqingLyu wants to merge 5 commits into
mainfrom
fork-pr-62070-fix-openai-compat-image-sanitization-and-streaming-error

Conversation

@BingqingLyu

@BingqingLyu BingqingLyu commented Apr 28, 2026

Copy link
Copy Markdown
Owner

Summary

Fixes openclaw#59913

  • Bug 1: Gateway-provided images (via OpenAI compat /v1/chat/completions image_url parts) bypassed the sanitization/resize pipeline. detectAndLoadPromptImages() early-returned existingImages unsanitized when no image refs were found in the prompt text. Large images (e.g. Mac Retina screenshots >5MB) were forwarded to Anthropic at full resolution, hitting the 5MB per-image limit.
  • Bug 2: In streaming mode (stream: true), when a lifecycle "error" event fired before the async catch block could run, the stream was closed with only data: [DONE] — the error was completely swallowed and the client received no error content.

Changes

  • src/agents/pi-embedded-runner/run/images.ts: Apply sanitizeImagesWithLog() to existingImages on the early-return path (no prompt image refs detected)
  • src/gateway/openai-http.ts: Write error content chunk in the lifecycle "error" handler before closing the stream, when no assistant content was already sent

Test plan

  • New test: existingImages are sanitized even when prompt has no image references (images.test.ts)
  • New test: error content appears in SSE stream when lifecycle error fires without prior assistant deltas (openai-http.test.ts)
  • All 32 existing image tests pass
  • All 6 existing gateway openai-http tests pass

AI-assisted: Built with Claude Code. Fully tested locally. I understand what the code does.

🤖 Generated with Claude Code

htplbc and others added 5 commits April 6, 2026 11:33
…ompat endpoint

Gateway-provided images (via OpenAI compat /v1/chat/completions) bypassed
the sanitization/resize pipeline because detectAndLoadPromptImages() early-
returned existingImages unsanitized when no image refs were found in prompt
text. Large images (e.g. Mac Retina screenshots) hit Anthropic's 5MB limit.

Additionally, in streaming mode, when a lifecycle "error" event fired before
the async catch block, the stream was closed with only [DONE] and no error
content — silently swallowing the failure.

Fixes openclaw#59913

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…or path

Set sawAssistantDelta before emitting the lifecycle error event in the catch
block, so the lifecycle handler skips writing a second error chunk.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.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.

OpenAI compat gateway images bypass sanitization/resize, hit Anthropic 5MB limit

2 participants