Skip to content

fix: use app-chat REST as primary image generation, fallback to ws_imagine#374

Merged
chenyme merged 4 commits intochenyme:mainfrom
JinchengGao-Infty:fix-image-gen-app-chat
Mar 23, 2026
Merged

fix: use app-chat REST as primary image generation, fallback to ws_imagine#374
chenyme merged 4 commits intochenyme:mainfrom
JinchengGao-Infty:fix-image-gen-app-chat

Conversation

@JinchengGao-Infty
Copy link
Copy Markdown
Contributor

@JinchengGao-Infty JinchengGao-Infty commented Mar 20, 2026

Summary

  • The WebSocket endpoint wss://grok.com/ws/imagine/listen has been deprecated by Grok, causing all image generation requests to fail with rate_limit_exceeded errors
  • This PR switches the primary image generation path to the app-chat REST API (using GrokChatService with imageGen tool override), and keeps ws_imagine as a fallback

Changes

  • 功能新增
  • Bug 修复
  • 重构/清理
  • 文档更新
  • 其他(功能修复)

app/services/grok/services/image.py

  • Added _stream_app_chat() and _collect_app_chat() methods for app-chat based image generation
  • Added _app_chat_request_overrides() helper for imageGenerationCount and enableNsfw
  • Modified generate() to try app-chat first, then fallback to ws_imagine on non-rate-limit errors
  • Reuses ImageStreamProcessor / ImageCollectProcessor from image_edit module

app/services/grok/services/chat.py

  • Added request_overrides parameter to GrokChatService.chat() to pass through custom request fields

app/services/reverse/app_chat.py

  • Added request_overrides parameter to build_payload() and request()
  • Merges overrides into the payload before sending

Test plan

  • Verified image generation works via /v1/images/generations endpoint (both stream and non-stream)
  • Verified image generation works via /v1/chat/completions with image model
  • Confirmed ws_imagine fallback still functions when app-chat fails

Related Issues

Nothing

Verification

  • 本地运行验证
  • 单元/集成测试
  • Docker 构建通过
  • 未验证(请说明原因)

Breaking Changes

  • 有(请说明迁移方式)

…ck to ws_imagine

The WebSocket endpoint `wss://grok.com/ws/imagine/listen` has been deprecated
by Grok, causing all image generation requests to fail with `rate_limit_exceeded`.

This change:
- Adds `_stream_app_chat` and `_collect_app_chat` methods that use the app-chat
  REST API (via `GrokChatService`) with `imageGen` tool override for image generation
- Makes app-chat the primary path for both streaming and non-streaming image generation
- Falls back to ws_imagine only when app-chat fails with a non-rate-limit error
- Adds `request_overrides` parameter to `GrokChatService.chat()` and
  `AppChatReverse.build_payload()` to support `imageGenerationCount` and `enableNsfw`
- Reuses existing `ImageStreamProcessor` and `ImageCollectProcessor` from image_edit
  module for processing app-chat image responses

Fixes image generation that was broken for all users after Grok deprecated ws_imagine.
…ng, extract markdown images

image_edit.py:
- Remove VideoService/parentPostId dependency (no longer needed)
- Use file_attachments instead of imageEditUri for image upload
- Use fixed grok-4 / MODEL_MODE_AUTO for edit upstream
- Add image_id to stream events for frontend waterfall compatibility

app_chat.py:
- Add robust _read_error_body() for non-200 upstream responses
- Log content-type and body on failure for better debugging

chat.py:
- Extract image URLs from markdown syntax in message content
- Deduplicate collected image URLs
@RoadMillion
Copy link
Copy Markdown

Hurry up and release it, buddy.

@secondarycoder
Copy link
Copy Markdown

Video generation is having the same issue.

@JinchengGao-Infty
Copy link
Copy Markdown
Contributor Author

Hurry up and release it, buddy.

Here's my fork with Docker support, feel free to use it: https://github.com/JinchengGao-Infty/grok2api

@JinchengGao-Infty
Copy link
Copy Markdown
Contributor Author

Video generation is having the same issue.

That's a separate issue — free accounts can't generate videos anymore.

piexian added a commit to piexian/grok2api that referenced this pull request Mar 22, 2026
上游来源与合并方式:

- chenyme#366 chenyme#366 :手工移植 usage 估算与 responses usage 映射,按当前主线重接 chat/responses 接口,没有直接套用原 patch。

- chenyme#374 chenyme#374 :参考原始思路改为 app-chat 单链路接入,移除已不存在的 ws 回退,并补齐 request_overrides。

- chenyme#375 chenyme#375 :参考原始思路重写视频续写与 token 清理,不使用 localhost 自调用,直接复用现有 app-chat 请求链路,并处理 email-domain-rejected。

- chenyme#336 chenyme#336 :手工合并多图参考视频支持,保持旧 image_reference/image_url 兼容,仅在多图或 @图N 场景启用新链路。
@dhruvin771
Copy link
Copy Markdown

Video generation is having the same issue.

That's a separate issue — free accounts can't generate videos anymore.

Do you know any free alternative to Grok that can generate videos like this?

@shawn8172014
Copy link
Copy Markdown

貌似无法渲染(cherrystudio)

@shawn8172014
Copy link
Copy Markdown

@JinchengGao-Infty 貌似无法渲染(cherrystudio) 还需要配置什么?

@keung67
Copy link
Copy Markdown
Contributor

keung67 commented Mar 23, 2026

Screenshot_2026-03-23-12-17-51-07_40deb401b9ffe8e1df2f1cc5ba480b12

Screenshot_2026-03-23-13-32-48-80_74158c69f0af68fbf38b28b8774cd491

官網及app測試有限流
app-chat 不丟429而直接空回覆
帳號冷卻時間似乎是24小時
新帳號5次對話生成10張圖片
但大量普通basicsso帳號也許仍可用
但要處理遇到空回覆就換號的邏輯

@JinchengGao-Infty
Copy link
Copy Markdown
Contributor Author

@JinchengGao-Infty 貌似无法渲染(cherrystudio) 还需要配置什么?

配置管理 -> 启动地址,填自己的

@eskimo-baby
Copy link
Copy Markdown

openclaw 里可以使用生成图片吗?

@chenyme chenyme merged commit 16e37b1 into chenyme:main Mar 23, 2026
8 of 9 checks passed
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.

8 participants