Skip to content

fix(azure): force upstream SSE for streamed image gen - #2122

Merged
steebchen merged 1 commit into
mainfrom
fix-azure-stream-image
Apr 30, 2026
Merged

steebchen merged 1 commit into
mainfrom
fix-azure-stream-image

Conversation

@steebchen

@steebchen steebchen commented Apr 30, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Azure gpt-image-2 with stream=true never returned a final image because forceImageStreamUpstream was gated on !stream.
  • For image generation the upstream request is always non-streaming (effectiveStream is forced false when faking streaming for the client), so without partial_images=1 the request hit Azure's 122s synchronous wall and timed out.
  • Drop the !stream gate in both call sites so upstream SSE injection (and the longer streaming timeout) applies whether or not the client requested streaming. The existing collapseImageGenSse + fakeStreamingForImageGen wiring already handles re-wrapping the response for streaming clients.

Test plan

  • Streaming request to Azure gpt-image-2 returns the final image instead of timing out
  • Non-streaming request to Azure gpt-image-2 still works (regression check)
  • OpenAI gpt-image-* streaming and non-streaming still work

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Standardized image generation streaming for OpenAI and Azure models to ensure consistent upstream handling across all client requests, improving reliability for image generation tasks.

Azure gpt-image-2 with stream=true never returned a final image because
forceImageStreamUpstream was gated on !stream. For image generation the
upstream request is always non-streaming (effectiveStream is forced false
when faking streaming for the client), so without partial_images=1 the
request hit Azure's 122s synchronous wall and timed out.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 30, 2026 13:02
@coderabbitai

coderabbitai Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 89ed212e-157b-446d-8abb-62b048697856

📥 Commits

Reviewing files that changed from the base of the PR and between fa2553b and 1843c3e.

📒 Files selected for processing (1)
  • apps/gateway/src/chat/chat.ts

Walkthrough

The logic for forcing upstream SSE (Server-Sent Events) in image generation requests is modified to always apply when using OpenAI or Azure providers for image models, regardless of the client's streaming preference. This behavior is now consistently applied across the main execution path and retry/fallback paths.

Changes

Cohort / File(s) Summary
Image Stream Upstream Forcing
apps/gateway/src/chat/chat.ts
Modified condition for forceImageStreamUpstream to apply unconditionally for OpenAI/Azure image generation models rather than only when client requests non-streaming. Updated explanatory comment and aligned retry/fallback path to use consistent forcing behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: forcing upstream SSE for streamed image generation on Azure/OpenAI, addressing the core issue preventing Azure gpt-image-2 with stream=true from returning final images.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-azure-stream-image

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 6/8 reviews remaining, refill in 8 minutes and 50 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes Azure gpt-image-2 image-generation requests with stream=true by ensuring the gateway always forces upstream SSE (stream=true + partial_images=1) for OpenAI/Azure gpt-image-*, preventing Azure’s synchronous ~122s wall from causing timeouts and allowing the longer streaming timeout to apply.

Changes:

  • Remove the !stream gate so forceImageStreamUpstream applies regardless of client streaming preference.
  • Apply the same logic in both the initial request setup and the retry/provider-context re-resolution path.
  • Update inline comments to reflect the corrected behavior and rationale.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +3722 to 3732
// Force upstream SSE for OpenAI/Azure gpt-image-* regardless of what the client
// requested. For image generation the upstream request is always non-streaming
// (effectiveStream is forced false above when faking streaming for the client),
// so partial_images=1 is needed in both cases to keep the connection alive past
// Azure's 122s synchronous wall and to use AI_STREAMING_TIMEOUT_MS (240s default)
// instead of AI_TIMEOUT_MS (180s). The SSE response is collapsed back into the
// regular non-streaming JSON shape before being returned (or re-wrapped as fake
// SSE for clients that requested streaming).
let forceImageStreamUpstream =
!stream &&
isImageGeneration &&
(usedProvider === "openai" || usedProvider === "azure");
@steebchen
steebchen added this pull request to the merge queue Apr 30, 2026
Merged via the queue into main with commit 9e0ff50 Apr 30, 2026
21 checks passed
@steebchen
steebchen deleted the fix-azure-stream-image branch April 30, 2026 13:18
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.

2 participants