Skip to content

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

Open
JinchengGao-Infty wants to merge 2 commits intochenyme:mainfrom
JinchengGao-Infty:fix-image-gen-app-chat
Open

fix: use app-chat REST as primary image generation, fallback to ws_imagine#374
JinchengGao-Infty wants to merge 2 commits intochenyme:mainfrom
JinchengGao-Infty:fix-image-gen-app-chat

Conversation

@JinchengGao-Infty
Copy link

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

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

…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

Hurry up and release it, buddy.

@secondarycoder
Copy link

Video generation is having the same issue.

@JinchengGao-Infty
Copy link
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
Author

Video generation is having the same issue.

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

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.

3 participants