[GH-ISSUE #11931] feat: Models that support image generation, such as grok-2-image-1212 #31934

Closed
opened 2026-04-25 05:49:34 -05:00 by GiteaMirror · 15 comments
Owner

Originally created by @lovepiece on GitHub (Mar 21, 2025).
Original GitHub issue: https://github.com/open-webui/open-webui/issues/11931

Check Existing Issues

  • I have searched the existing issues and discussions.

Problem Description

The grok api introduces a model for image generation. It seems that the Grok-2-image-1212 model is not supported by openwebui yet.
About model is introduced in the website: https://docs.x.ai/docs/guides/image-generations#parameters
Can you add support for grok's image generation model?

Desired Solution you'd like

import os
from openai import OpenAI

XAI_API_KEY = "xai-xxxxx"

client = OpenAI(base_url="https://api.x.ai/v1", api_key=XAI_API_KEY)

response = client.images.generate(
    model="grok-2-image",
    prompt="draw a cat"
)

print(response.data[0].url)

This is the use of python example, Is it possible to display the connected picture in the dialog box after the generation?

Alternatives Considered

Solution

Additional Context

No response

Originally created by @lovepiece on GitHub (Mar 21, 2025). Original GitHub issue: https://github.com/open-webui/open-webui/issues/11931 ### Check Existing Issues - [x] I have searched the existing issues and discussions. ### Problem Description The grok api introduces a model for image generation. It seems that the Grok-2-image-1212 model is not supported by openwebui yet. About model is introduced in the website: https://docs.x.ai/docs/guides/image-generations#parameters Can you add support for grok's image generation model? ### Desired Solution you'd like ``` import os from openai import OpenAI XAI_API_KEY = "xai-xxxxx" client = OpenAI(base_url="https://api.x.ai/v1", api_key=XAI_API_KEY) response = client.images.generate( model="grok-2-image", prompt="draw a cat" ) print(response.data[0].url) ``` This is the use of python example, Is it possible to display the connected picture in the dialog box after the generation? ### Alternatives Considered [Solution](https://github.com/open-webui/open-webui/issues/11931#issuecomment-2833483981) ### Additional Context _No response_
Author
Owner

@tjbck commented on GitHub (Mar 21, 2025):

They're supported.

<!-- gh-comment-id:2744275804 --> @tjbck commented on GitHub (Mar 21, 2025): They're supported.
Author
Owner

@tan-yong-sheng commented on GitHub (Mar 22, 2025):

Hi @tjbck any guides for that? I couldn't find any docs on LiteLLM on this, thanks.

<!-- gh-comment-id:2745239660 --> @tan-yong-sheng commented on GitHub (Mar 22, 2025): Hi @tjbck any guides for that? I couldn't find any docs on LiteLLM on this, thanks.
Author
Owner

@Classic298 commented on GitHub (Mar 22, 2025):

@tan-yong-sheng Set up the model in LiteLLM, use OpenAI endpoint in image generation and point to your LiteLLM proxy server and set up the image generation model. Done. If LiteLLM has no docs on grok's specific model, perhaps that's because they don't support it yet.

OpenWebUI itself does support any image model as long as it's accessible by one of the supported Image Generation endpoints. If LiteLLM doesn't support Grok yet, wait a few days or a week, they are usually fast with implementing popular models. But that's an issue of LiteLLM then, not OpenWebUI

<!-- gh-comment-id:2745358152 --> @Classic298 commented on GitHub (Mar 22, 2025): @tan-yong-sheng Set up the model in LiteLLM, use OpenAI endpoint in image generation and point to your LiteLLM proxy server and set up the image generation model. Done. If LiteLLM has no docs on grok's specific model, perhaps that's because they don't support it yet. OpenWebUI itself does support any image model as long as it's accessible by one of the supported Image Generation endpoints. If LiteLLM doesn't support Grok yet, wait a few days or a week, they are usually fast with implementing popular models. But that's an issue of LiteLLM then, not OpenWebUI
Author
Owner

@tan-yong-sheng commented on GitHub (Mar 22, 2025):

Thanks for answering. Understood, thanks a lot. Btw, my typo for previous message. It should be openwebui instead of litellm

<!-- gh-comment-id:2745376917 --> @tan-yong-sheng commented on GitHub (Mar 22, 2025): Thanks for answering. Understood, thanks a lot. Btw, my typo for previous message. It should be openwebui instead of litellm
Author
Owner

@t0saki commented on GitHub (Mar 22, 2025):

Using grok-2-image in xAI's API directly in OWUI results in the message The size parameter is not supported at the moment. Leave it empty., but OWUI does not allow leaving the resolution blank. I've created a tool that uses Cloudflare Workers to filter out unsupported parameters from the requests.
xAI-Image-Gen-API-Refine

<!-- gh-comment-id:2745417418 --> @t0saki commented on GitHub (Mar 22, 2025): Using grok-2-image in xAI's API directly in OWUI results in the message `The size parameter is not supported at the moment. Leave it empty.`, but OWUI does not allow leaving the resolution blank. I've created a tool that uses Cloudflare Workers to filter out unsupported parameters from the requests. [xAI-Image-Gen-API-Refine](https://github.com/t0saki/xAI-Image-Gen-API-Refine/blob/master/README-en.md)
Author
Owner

@recklessop commented on GitHub (Apr 17, 2025):

Using grok-2-image in xAI's API directly in OWUI results in the message The size parameter is not supported at the moment. Leave it empty., but OWUI does not allow leaving the resolution blank. I've created a tool that uses Cloudflare Workers to filter out unsupported parameters from the requests. xAI-Image-Gen-API-Refine

I am seeing the same problem. But why do i want to run yet another thing? why cant Open-webui just support not sending a size?

<!-- gh-comment-id:2811377078 --> @recklessop commented on GitHub (Apr 17, 2025): > Using grok-2-image in xAI's API directly in OWUI results in the message `The size parameter is not supported at the moment. Leave it empty.`, but OWUI does not allow leaving the resolution blank. I've created a tool that uses Cloudflare Workers to filter out unsupported parameters from the requests. [xAI-Image-Gen-API-Refine](https://github.com/t0saki/xAI-Image-Gen-API-Refine/blob/master/README-en.md) I am seeing the same problem. But why do i want to run yet another thing? why cant Open-webui just support not sending a size?
Author
Owner

@t0saki commented on GitHub (Apr 17, 2025):

Using grok-2-image in xAI's API directly in OWUI results in the message The size parameter is not supported at the moment. Leave it empty., but OWUI does not allow leaving the resolution blank. I've created a tool that uses Cloudflare Workers to filter out unsupported parameters from the requests. xAI-Image-Gen-API-Refine

I am seeing the same problem. But why do i want to run yet another thing? why cant Open-webui just support not sending a size?

I believe it would be a small fix to let Open-WebUI support sending without some params, but writing a script and deploy it to clouflare is faster to me. It cost me less than a hour, but if I want to modify the OWUI, compiling and reviewing PR may takes longer time.

<!-- gh-comment-id:2812700129 --> @t0saki commented on GitHub (Apr 17, 2025): > > Using grok-2-image in xAI's API directly in OWUI results in the message `The size parameter is not supported at the moment. Leave it empty.`, but OWUI does not allow leaving the resolution blank. I've created a tool that uses Cloudflare Workers to filter out unsupported parameters from the requests. [xAI-Image-Gen-API-Refine](https://github.com/t0saki/xAI-Image-Gen-API-Refine/blob/master/README-en.md) > > I am seeing the same problem. But why do i want to run yet another thing? why cant Open-webui just support not sending a size? I believe it would be a small fix to let Open-WebUI support sending without some params, but writing a script and deploy it to clouflare is faster to me. It cost me less than a hour, but if I want to modify the OWUI, compiling and reviewing PR may takes longer time.
Author
Owner

@wzmzw commented on GitHub (Apr 26, 2025):

Thanks for answering. Understood, thanks a lot. Btw, my typo for previous message. It should be openwebui instead of litellm

Hi, after I imported Grok-API in OWUI, I can select the grok-2-image-1212 model, but after entering the prompt word, it shows OpenAI: Network Problem. Have you solved this problem?

<!-- gh-comment-id:2831650161 --> @wzmzw commented on GitHub (Apr 26, 2025): > Thanks for answering. Understood, thanks a lot. Btw, my typo for previous message. It should be openwebui instead of litellm Hi, after I imported Grok-API in OWUI, I can select the grok-2-image-1212 model, but after entering the prompt word, it shows OpenAI: Network Problem. Have you solved this problem?
Author
Owner

@tan-yong-sheng commented on GitHub (Apr 26, 2025):

Hi, no, I didn't use litellm for serving image generation endpoint in the end.

I just stick to solution instead: https://github.com/open-webui/open-webui/issues/11931#issuecomment-2745417418

<!-- gh-comment-id:2831807083 --> @tan-yong-sheng commented on GitHub (Apr 26, 2025): Hi, no, I didn't use litellm for serving image generation endpoint in the end. I just stick to solution instead: https://github.com/open-webui/open-webui/issues/11931#issuecomment-2745417418
Author
Owner

@lovepiece commented on GitHub (Apr 27, 2025):

I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image.

In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running.

import http.server
import socketserver
import requests
import json
import time

class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_POST(self):
        # 读取请求内容
        content_length = int(self.headers.get('Content-Length', 0))
        request_body = self.rfile.read(content_length)
        
        # 解析请求体
        try:
            request_data = json.loads(request_body.decode('utf-8'))
        except json.JSONDecodeError:
            print("Error: Invalid JSON in request body")
            self.send_error(400, "Invalid JSON in request body")
            return
        
        # 修改请求数据
        request_data['model'] = "grok-2-image"
        request_data['response_format'] = "b64_json"
        
        # 删除size参数
        request_data.pop('size', None)
        
        target_url = "https://api.x.ai/v1/images/generations"
        headers = {
            "Authorization": self.headers.get('Authorization', ''),
            "Content-Type": "application/json"
        }
        
        try:
            response = requests.post(
                target_url,
                headers=headers,
                data=json.dumps(request_data)
            )
            
            # 解析原始响应
            original_response = response.json()
            
            # 修改响应格式,仅添加created字段
            modified_response = original_response.copy()
            modified_response['created'] = int(time.time())
            
            # 发送修改后的响应回给原始请求者
            self.send_response(response.status_code)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(json.dumps(modified_response).encode('utf-8'))
        
        except requests.RequestException as e:
            print(f"\nError forwarding request: {e}")
            self.send_error(500, "Error forwarding request")
        except (KeyError, IndexError, json.JSONDecodeError) as e:
            print(f"\nError processing response: {e}")
            self.send_error(500, "Error processing response")

    def log_message(self, format, *args):
        # 禁用默认的日志输出
        return

# 设置服务器地址和端口
PORT = 9988
Handler = RequestHandler

# 创建并运行服务器
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print(f"Serving at port {PORT}")
    httpd.serve_forever()
<!-- gh-comment-id:2833483981 --> @lovepiece commented on GitHub (Apr 27, 2025): I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image. In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running. ``` import http.server import socketserver import requests import json import time class RequestHandler(http.server.SimpleHTTPRequestHandler): def do_POST(self): # 读取请求内容 content_length = int(self.headers.get('Content-Length', 0)) request_body = self.rfile.read(content_length) # 解析请求体 try: request_data = json.loads(request_body.decode('utf-8')) except json.JSONDecodeError: print("Error: Invalid JSON in request body") self.send_error(400, "Invalid JSON in request body") return # 修改请求数据 request_data['model'] = "grok-2-image" request_data['response_format'] = "b64_json" # 删除size参数 request_data.pop('size', None) target_url = "https://api.x.ai/v1/images/generations" headers = { "Authorization": self.headers.get('Authorization', ''), "Content-Type": "application/json" } try: response = requests.post( target_url, headers=headers, data=json.dumps(request_data) ) # 解析原始响应 original_response = response.json() # 修改响应格式,仅添加created字段 modified_response = original_response.copy() modified_response['created'] = int(time.time()) # 发送修改后的响应回给原始请求者 self.send_response(response.status_code) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps(modified_response).encode('utf-8')) except requests.RequestException as e: print(f"\nError forwarding request: {e}") self.send_error(500, "Error forwarding request") except (KeyError, IndexError, json.JSONDecodeError) as e: print(f"\nError processing response: {e}") self.send_error(500, "Error processing response") def log_message(self, format, *args): # 禁用默认的日志输出 return # 设置服务器地址和端口 PORT = 9988 Handler = RequestHandler # 创建并运行服务器 with socketserver.TCPServer(("", PORT), Handler) as httpd: print(f"Serving at port {PORT}") httpd.serve_forever() ```
Author
Owner

@wzmzw commented on GitHub (Apr 27, 2025):

I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image.

In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running.

import http.server
import socketserver
import requests
import json
import time

class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_POST(self):
        # 读取请求内容
        content_length = int(self.headers.get('Content-Length', 0))
        request_body = self.rfile.read(content_length)
        
        # 解析请求体
        try:
            request_data = json.loads(request_body.decode('utf-8'))
        except json.JSONDecodeError:
            print("Error: Invalid JSON in request body")
            self.send_error(400, "Invalid JSON in request body")
            return
        
        # 修改请求数据
        request_data['model'] = "grok-2-image"
        request_data['response_format'] = "b64_json"
        
        # 删除size参数
        request_data.pop('size', None)
        
        target_url = "https://api.x.ai/v1/images/generations"
        headers = {
            "Authorization": self.headers.get('Authorization', ''),
            "Content-Type": "application/json"
        }
        
        try:
            response = requests.post(
                target_url,
                headers=headers,
                data=json.dumps(request_data)
            )
            
            # 解析原始响应
            original_response = response.json()
            
            # 修改响应格式,仅添加created字段
            modified_response = original_response.copy()
            modified_response['created'] = int(time.time())
            
            # 发送修改后的响应回给原始请求者
            self.send_response(response.status_code)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(json.dumps(modified_response).encode('utf-8'))
        
        except requests.RequestException as e:
            print(f"\nError forwarding request: {e}")
            self.send_error(500, "Error forwarding request")
        except (KeyError, IndexError, json.JSONDecodeError) as e:
            print(f"\nError processing response: {e}")
            self.send_error(500, "Error processing response")

    def log_message(self, format, *args):
        # 禁用默认的日志输出
        return

# 设置服务器地址和端口
PORT = 9988
Handler = RequestHandler

# 创建并运行服务器
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print(f"Serving at port {PORT}")
    httpd.serve_forever()

Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment.

<!-- gh-comment-id:2833558068 --> @wzmzw commented on GitHub (Apr 27, 2025): > I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image. > > In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running. > > ``` > import http.server > import socketserver > import requests > import json > import time > > class RequestHandler(http.server.SimpleHTTPRequestHandler): > def do_POST(self): > # 读取请求内容 > content_length = int(self.headers.get('Content-Length', 0)) > request_body = self.rfile.read(content_length) > > # 解析请求体 > try: > request_data = json.loads(request_body.decode('utf-8')) > except json.JSONDecodeError: > print("Error: Invalid JSON in request body") > self.send_error(400, "Invalid JSON in request body") > return > > # 修改请求数据 > request_data['model'] = "grok-2-image" > request_data['response_format'] = "b64_json" > > # 删除size参数 > request_data.pop('size', None) > > target_url = "https://api.x.ai/v1/images/generations" > headers = { > "Authorization": self.headers.get('Authorization', ''), > "Content-Type": "application/json" > } > > try: > response = requests.post( > target_url, > headers=headers, > data=json.dumps(request_data) > ) > > # 解析原始响应 > original_response = response.json() > > # 修改响应格式,仅添加created字段 > modified_response = original_response.copy() > modified_response['created'] = int(time.time()) > > # 发送修改后的响应回给原始请求者 > self.send_response(response.status_code) > self.send_header("Content-Type", "application/json") > self.end_headers() > self.wfile.write(json.dumps(modified_response).encode('utf-8')) > > except requests.RequestException as e: > print(f"\nError forwarding request: {e}") > self.send_error(500, "Error forwarding request") > except (KeyError, IndexError, json.JSONDecodeError) as e: > print(f"\nError processing response: {e}") > self.send_error(500, "Error processing response") > > def log_message(self, format, *args): > # 禁用默认的日志输出 > return > > # 设置服务器地址和端口 > PORT = 9988 > Handler = RequestHandler > > # 创建并运行服务器 > with socketserver.TCPServer(("", PORT), Handler) as httpd: > print(f"Serving at port {PORT}") > httpd.serve_forever() > ``` Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment.
Author
Owner

@lovepiece commented on GitHub (Apr 27, 2025):

I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image.
In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running.

import http.server
import socketserver
import requests
import json
import time

class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_POST(self):
        # 读取请求内容
        content_length = int(self.headers.get('Content-Length', 0))
        request_body = self.rfile.read(content_length)
        
        # 解析请求体
        try:
            request_data = json.loads(request_body.decode('utf-8'))
        except json.JSONDecodeError:
            print("Error: Invalid JSON in request body")
            self.send_error(400, "Invalid JSON in request body")
            return
        
        # 修改请求数据
        request_data['model'] = "grok-2-image"
        request_data['response_format'] = "b64_json"
        
        # 删除size参数
        request_data.pop('size', None)
        
        target_url = "https://api.x.ai/v1/images/generations"
        headers = {
            "Authorization": self.headers.get('Authorization', ''),
            "Content-Type": "application/json"
        }
        
        try:
            response = requests.post(
                target_url,
                headers=headers,
                data=json.dumps(request_data)
            )
            
            # 解析原始响应
            original_response = response.json()
            
            # 修改响应格式,仅添加created字段
            modified_response = original_response.copy()
            modified_response['created'] = int(time.time())
            
            # 发送修改后的响应回给原始请求者
            self.send_response(response.status_code)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(json.dumps(modified_response).encode('utf-8'))
        
        except requests.RequestException as e:
            print(f"\nError forwarding request: {e}")
            self.send_error(500, "Error forwarding request")
        except (KeyError, IndexError, json.JSONDecodeError) as e:
            print(f"\nError processing response: {e}")
            self.send_error(500, "Error processing response")

    def log_message(self, format, *args):
        # 禁用默认的日志输出
        return

# 设置服务器地址和端口
PORT = 9988
Handler = RequestHandler

# 创建并运行服务器
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print(f"Serving at port {PORT}")
    httpd.serve_forever()

Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment.

Set it in the OPENAI API in the image generation settings, for example: http://192.168.10.11:9988/v1, this address is the server address running the python script,and fill in your own grok api for the secret key

<!-- gh-comment-id:2833560199 --> @lovepiece commented on GitHub (Apr 27, 2025): > > I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image. > > In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running. > > ``` > > import http.server > > import socketserver > > import requests > > import json > > import time > > > > class RequestHandler(http.server.SimpleHTTPRequestHandler): > > def do_POST(self): > > # 读取请求内容 > > content_length = int(self.headers.get('Content-Length', 0)) > > request_body = self.rfile.read(content_length) > > > > # 解析请求体 > > try: > > request_data = json.loads(request_body.decode('utf-8')) > > except json.JSONDecodeError: > > print("Error: Invalid JSON in request body") > > self.send_error(400, "Invalid JSON in request body") > > return > > > > # 修改请求数据 > > request_data['model'] = "grok-2-image" > > request_data['response_format'] = "b64_json" > > > > # 删除size参数 > > request_data.pop('size', None) > > > > target_url = "https://api.x.ai/v1/images/generations" > > headers = { > > "Authorization": self.headers.get('Authorization', ''), > > "Content-Type": "application/json" > > } > > > > try: > > response = requests.post( > > target_url, > > headers=headers, > > data=json.dumps(request_data) > > ) > > > > # 解析原始响应 > > original_response = response.json() > > > > # 修改响应格式,仅添加created字段 > > modified_response = original_response.copy() > > modified_response['created'] = int(time.time()) > > > > # 发送修改后的响应回给原始请求者 > > self.send_response(response.status_code) > > self.send_header("Content-Type", "application/json") > > self.end_headers() > > self.wfile.write(json.dumps(modified_response).encode('utf-8')) > > > > except requests.RequestException as e: > > print(f"\nError forwarding request: {e}") > > self.send_error(500, "Error forwarding request") > > except (KeyError, IndexError, json.JSONDecodeError) as e: > > print(f"\nError processing response: {e}") > > self.send_error(500, "Error processing response") > > > > def log_message(self, format, *args): > > # 禁用默认的日志输出 > > return > > > > # 设置服务器地址和端口 > > PORT = 9988 > > Handler = RequestHandler > > > > # 创建并运行服务器 > > with socketserver.TCPServer(("", PORT), Handler) as httpd: > > print(f"Serving at port {PORT}") > > httpd.serve_forever() > > ``` > > Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment. Set it in the OPENAI API in the image generation settings, for example: http://192.168.10.11:9988/v1, this address is the server address running the python script,and fill in your own grok api for the secret key
Author
Owner

@wzmzw commented on GitHub (Apr 27, 2025):

I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image.
In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running.

import http.server
import socketserver
import requests
import json
import time

class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_POST(self):
        # 读取请求内容
        content_length = int(self.headers.get('Content-Length', 0))
        request_body = self.rfile.read(content_length)
        
        # 解析请求体
        try:
            request_data = json.loads(request_body.decode('utf-8'))
        except json.JSONDecodeError:
            print("Error: Invalid JSON in request body")
            self.send_error(400, "Invalid JSON in request body")
            return
        
        # 修改请求数据
        request_data['model'] = "grok-2-image"
        request_data['response_format'] = "b64_json"
        
        # 删除size参数
        request_data.pop('size', None)
        
        target_url = "https://api.x.ai/v1/images/generations"
        headers = {
            "Authorization": self.headers.get('Authorization', ''),
            "Content-Type": "application/json"
        }
        
        try:
            response = requests.post(
                target_url,
                headers=headers,
                data=json.dumps(request_data)
            )
            
            # 解析原始响应
            original_response = response.json()
            
            # 修改响应格式,仅添加created字段
            modified_response = original_response.copy()
            modified_response['created'] = int(time.time())
            
            # 发送修改后的响应回给原始请求者
            self.send_response(response.status_code)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(json.dumps(modified_response).encode('utf-8'))
        
        except requests.RequestException as e:
            print(f"\nError forwarding request: {e}")
            self.send_error(500, "Error forwarding request")
        except (KeyError, IndexError, json.JSONDecodeError) as e:
            print(f"\nError processing response: {e}")
            self.send_error(500, "Error processing response")

    def log_message(self, format, *args):
        # 禁用默认的日志输出
        return

# 设置服务器地址和端口
PORT = 9988
Handler = RequestHandler

# 创建并运行服务器
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print(f"Serving at port {PORT}")
    httpd.serve_forever()

Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment.

Set it in the OPENAI API in the image generation settings, for example: http://192.168.10.11:9988/v1, this address is the server address running the python script,and fill in your own grok api for the secret key

I have tried changing this option, including the local address and http://localhost:9988/v1 as you said. My problem still occurs. So how do you set up your Grok API? Is it All endpoints and All models or just images?

<!-- gh-comment-id:2833567273 --> @wzmzw commented on GitHub (Apr 27, 2025): > > > I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image. > > > In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running. > > > ``` > > > import http.server > > > import socketserver > > > import requests > > > import json > > > import time > > > > > > class RequestHandler(http.server.SimpleHTTPRequestHandler): > > > def do_POST(self): > > > # 读取请求内容 > > > content_length = int(self.headers.get('Content-Length', 0)) > > > request_body = self.rfile.read(content_length) > > > > > > # 解析请求体 > > > try: > > > request_data = json.loads(request_body.decode('utf-8')) > > > except json.JSONDecodeError: > > > print("Error: Invalid JSON in request body") > > > self.send_error(400, "Invalid JSON in request body") > > > return > > > > > > # 修改请求数据 > > > request_data['model'] = "grok-2-image" > > > request_data['response_format'] = "b64_json" > > > > > > # 删除size参数 > > > request_data.pop('size', None) > > > > > > target_url = "https://api.x.ai/v1/images/generations" > > > headers = { > > > "Authorization": self.headers.get('Authorization', ''), > > > "Content-Type": "application/json" > > > } > > > > > > try: > > > response = requests.post( > > > target_url, > > > headers=headers, > > > data=json.dumps(request_data) > > > ) > > > > > > # 解析原始响应 > > > original_response = response.json() > > > > > > # 修改响应格式,仅添加created字段 > > > modified_response = original_response.copy() > > > modified_response['created'] = int(time.time()) > > > > > > # 发送修改后的响应回给原始请求者 > > > self.send_response(response.status_code) > > > self.send_header("Content-Type", "application/json") > > > self.end_headers() > > > self.wfile.write(json.dumps(modified_response).encode('utf-8')) > > > > > > except requests.RequestException as e: > > > print(f"\nError forwarding request: {e}") > > > self.send_error(500, "Error forwarding request") > > > except (KeyError, IndexError, json.JSONDecodeError) as e: > > > print(f"\nError processing response: {e}") > > > self.send_error(500, "Error processing response") > > > > > > def log_message(self, format, *args): > > > # 禁用默认的日志输出 > > > return > > > > > > # 设置服务器地址和端口 > > > PORT = 9988 > > > Handler = RequestHandler > > > > > > # 创建并运行服务器 > > > with socketserver.TCPServer(("", PORT), Handler) as httpd: > > > print(f"Serving at port {PORT}") > > > httpd.serve_forever() > > > ``` > > > > > > Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment. > > Set it in the OPENAI API in the image generation settings, for example: http://192.168.10.11:9988/v1, this address is the server address running the python script,and fill in your own grok api for the secret key I have tried changing this option, including the local address and http://localhost:9988/v1 as you said. My problem still occurs. So how do you set up your Grok API? Is it All endpoints and All models or just images?
Author
Owner

@lovepiece commented on GitHub (Apr 28, 2025):

I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image.
In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running.

import http.server
import socketserver
import requests
import json
import time

class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_POST(self):
        # 读取请求内容
        content_length = int(self.headers.get('Content-Length', 0))
        request_body = self.rfile.read(content_length)
        
        # 解析请求体
        try:
            request_data = json.loads(request_body.decode('utf-8'))
        except json.JSONDecodeError:
            print("Error: Invalid JSON in request body")
            self.send_error(400, "Invalid JSON in request body")
            return
        
        # 修改请求数据
        request_data['model'] = "grok-2-image"
        request_data['response_format'] = "b64_json"
        
        # 删除size参数
        request_data.pop('size', None)
        
        target_url = "https://api.x.ai/v1/images/generations"
        headers = {
            "Authorization": self.headers.get('Authorization', ''),
            "Content-Type": "application/json"
        }
        
        try:
            response = requests.post(
                target_url,
                headers=headers,
                data=json.dumps(request_data)
            )
            
            # 解析原始响应
            original_response = response.json()
            
            # 修改响应格式,仅添加created字段
            modified_response = original_response.copy()
            modified_response['created'] = int(time.time())
            
            # 发送修改后的响应回给原始请求者
            self.send_response(response.status_code)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(json.dumps(modified_response).encode('utf-8'))
        
        except requests.RequestException as e:
            print(f"\nError forwarding request: {e}")
            self.send_error(500, "Error forwarding request")
        except (KeyError, IndexError, json.JSONDecodeError) as e:
            print(f"\nError processing response: {e}")
            self.send_error(500, "Error processing response")

    def log_message(self, format, *args):
        # 禁用默认的日志输出
        return

# 设置服务器地址和端口
PORT = 9988
Handler = RequestHandler

# 创建并运行服务器
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print(f"Serving at port {PORT}")
    httpd.serve_forever()

Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment.

Set it in the OPENAI API in the image generation settings, for example: http://192.168.10.11:9988/v1, this address is the server address running the python script,and fill in your own grok api for the secret key

I have tried changing this option, including the local address and http://localhost:9988/v1 as you said. My problem still occurs. So how do you set up your Grok API? Is it All endpoints and All models or just images?

Please make sure that your openwebui can connect to the address where python is running. If the openwebui is deployed by docker, you should confirm again whether the network can connect to the address where python is running. My API uses all the models.

<!-- gh-comment-id:2835359431 --> @lovepiece commented on GitHub (Apr 28, 2025): > > > > I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image. > > > > In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running. > > > > ``` > > > > import http.server > > > > import socketserver > > > > import requests > > > > import json > > > > import time > > > > > > > > class RequestHandler(http.server.SimpleHTTPRequestHandler): > > > > def do_POST(self): > > > > # 读取请求内容 > > > > content_length = int(self.headers.get('Content-Length', 0)) > > > > request_body = self.rfile.read(content_length) > > > > > > > > # 解析请求体 > > > > try: > > > > request_data = json.loads(request_body.decode('utf-8')) > > > > except json.JSONDecodeError: > > > > print("Error: Invalid JSON in request body") > > > > self.send_error(400, "Invalid JSON in request body") > > > > return > > > > > > > > # 修改请求数据 > > > > request_data['model'] = "grok-2-image" > > > > request_data['response_format'] = "b64_json" > > > > > > > > # 删除size参数 > > > > request_data.pop('size', None) > > > > > > > > target_url = "https://api.x.ai/v1/images/generations" > > > > headers = { > > > > "Authorization": self.headers.get('Authorization', ''), > > > > "Content-Type": "application/json" > > > > } > > > > > > > > try: > > > > response = requests.post( > > > > target_url, > > > > headers=headers, > > > > data=json.dumps(request_data) > > > > ) > > > > > > > > # 解析原始响应 > > > > original_response = response.json() > > > > > > > > # 修改响应格式,仅添加created字段 > > > > modified_response = original_response.copy() > > > > modified_response['created'] = int(time.time()) > > > > > > > > # 发送修改后的响应回给原始请求者 > > > > self.send_response(response.status_code) > > > > self.send_header("Content-Type", "application/json") > > > > self.end_headers() > > > > self.wfile.write(json.dumps(modified_response).encode('utf-8')) > > > > > > > > except requests.RequestException as e: > > > > print(f"\nError forwarding request: {e}") > > > > self.send_error(500, "Error forwarding request") > > > > except (KeyError, IndexError, json.JSONDecodeError) as e: > > > > print(f"\nError processing response: {e}") > > > > self.send_error(500, "Error processing response") > > > > > > > > def log_message(self, format, *args): > > > > # 禁用默认的日志输出 > > > > return > > > > > > > > # 设置服务器地址和端口 > > > > PORT = 9988 > > > > Handler = RequestHandler > > > > > > > > # 创建并运行服务器 > > > > with socketserver.TCPServer(("", PORT), Handler) as httpd: > > > > print(f"Serving at port {PORT}") > > > > httpd.serve_forever() > > > > ``` > > > > > > > > > Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment. > > > > > > Set it in the OPENAI API in the image generation settings, for example: http://192.168.10.11:9988/v1, this address is the server address running the python script,and fill in your own grok api for the secret key > > I have tried changing this option, including the local address and http://localhost:9988/v1 as you said. My problem still occurs. So how do you set up your Grok API? Is it All endpoints and All models or just images? Please make sure that your openwebui can connect to the address where python is running. If the openwebui is deployed by docker, you should confirm again whether the network can connect to the address where python is running. My API uses all the models.
Author
Owner

@wzmzw commented on GitHub (Apr 28, 2025):

I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image.
In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running.

import http.server
import socketserver
import requests
import json
import time

class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_POST(self):
        # 读取请求内容
        content_length = int(self.headers.get('Content-Length', 0))
        request_body = self.rfile.read(content_length)
        
        # 解析请求体
        try:
            request_data = json.loads(request_body.decode('utf-8'))
        except json.JSONDecodeError:
            print("Error: Invalid JSON in request body")
            self.send_error(400, "Invalid JSON in request body")
            return
        
        # 修改请求数据
        request_data['model'] = "grok-2-image"
        request_data['response_format'] = "b64_json"
        
        # 删除size参数
        request_data.pop('size', None)
        
        target_url = "https://api.x.ai/v1/images/generations"
        headers = {
            "Authorization": self.headers.get('Authorization', ''),
            "Content-Type": "application/json"
        }
        
        try:
            response = requests.post(
                target_url,
                headers=headers,
                data=json.dumps(request_data)
            )
            
            # 解析原始响应
            original_response = response.json()
            
            # 修改响应格式,仅添加created字段
            modified_response = original_response.copy()
            modified_response['created'] = int(time.time())
            
            # 发送修改后的响应回给原始请求者
            self.send_response(response.status_code)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(json.dumps(modified_response).encode('utf-8'))
        
        except requests.RequestException as e:
            print(f"\nError forwarding request: {e}")
            self.send_error(500, "Error forwarding request")
        except (KeyError, IndexError, json.JSONDecodeError) as e:
            print(f"\nError processing response: {e}")
            self.send_error(500, "Error processing response")

    def log_message(self, format, *args):
        # 禁用默认的日志输出
        return

# 设置服务器地址和端口
PORT = 9988
Handler = RequestHandler

# 创建并运行服务器
with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print(f"Serving at port {PORT}")
    httpd.serve_forever()

Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment.

Set it in the OPENAI API in the image generation settings, for example: http://192.168.10.11:9988/v1, this address is the server address running the python script,and fill in your own grok api for the secret key

I have tried changing this option, including the local address and http://localhost:9988/v1 as you said. My problem still occurs. So how do you set up your Grok API? Is it All endpoints and All models or just images?

Please make sure that your openwebui can connect to the address where python is running. If the openwebui is deployed by docker, you should confirm again whether the network can connect to the address where python is running. My API uses all the models.

Thanks for your help. I also realized the generation of multiple images. The following is based on your code modification.

import http.server
import socketserver
import requests
import json
import time
import re

# 配置
TARGET_URL = "https://api.x.ai/v1/images/generations"
PORT = 9988  # 本地监听端口

class ProxyHandler(http.server.BaseHTTPRequestHandler):
    def do_POST(self):
        # 读取 Open WebUI 发来的数据
        content_length = int(self.headers.get('Content-Length', 0))
        post_data = self.rfile.read(content_length)

        try:
            request_json = json.loads(post_data.decode('utf-8'))
        except json.JSONDecodeError:
            self.send_error(400, "Invalid JSON")
            return

        # 提取提示词
        prompt = request_json.get("messages", [{}])[-1].get("content", "")
        if not prompt:
            self.send_error(400, "No prompt found")
            return

        # 从提示词中提取图像数量
        match = re.search(r'(\d+)\s*张', prompt)
        if match:
            n = int(match.group(1))
            n = max(1, min(n, 10))  # 限制 n 的范围在 1 到 10
        else:
            n = 1  # 默认生成一张图像

        # 构建新的请求数据
        new_payload = {
            "model": "grok-2-image",
            "prompt": prompt,
            "response_format": "b64_json",
            "n": n
        }

        headers = {
            "Authorization": self.headers.get('Authorization', ''),
            "Content-Type": "application/json"
        }

        # 发送请求到 xAI 图像生成接口
        try:
            response = requests.post(
                TARGET_URL,
                headers=headers,
                json=new_payload,
                timeout=60
            )
            response.raise_for_status()
            res_json = response.json()

            # 构建返回给 Open WebUI 的响应数据
            images = res_json.get('data', [])
            if not images:
                self.send_error(502, "No image data received from xAI")
                return

            # 将所有图片合成一条Markdown消息
            image_markdown = ""
            for idx, image in enumerate(images):
                b64_image = image.get('b64_json', '')
                if b64_image:
                    image_markdown += f"![Image {idx+1}](data:image/png;base64,{b64_image})\n"

            modified_response = {
                "id": "fake-id",
                "object": "chat.completion",
                "created": int(time.time()),
                "choices": [{
                    "index": 0,
                    "message": {
                        "role": "assistant",
                        "content": image_markdown.strip()
                    },
                    "finish_reason": "stop"
                }],
                "usage": {
                    "prompt_tokens": 0,
                    "completion_tokens": 0,
                    "total_tokens": 0
                }
            }

            self.send_response(200)
            self.send_header('Content-Type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(modified_response).encode('utf-8'))

        except requests.RequestException as e:
            self.send_error(502, f"Error contacting xAI: {e}")

    def do_GET(self):
        if self.path == "/v1/models":
            models = {
                "object": "list",
                "data": [
                    {
                        "id": "grok-2-image",
                        "object": "model",
                        "created": int(time.time()),
                        "owned_by": "openai",
                        "permission": []
                    }
                ]
            }
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            self.wfile.write(json.dumps(models).encode('utf-8'))
        else:
            self.send_error(404, "Not Found")

    def log_message(self, format, *args):
        return

# 启动服务器
with socketserver.TCPServer(("", PORT), ProxyHandler) as httpd:
    print(f"✅ Proxy Server running at http://localhost:{PORT}")
    httpd.serve_forever()

<!-- gh-comment-id:2836044318 --> @wzmzw commented on GitHub (Apr 28, 2025): > > > > > I wrote a python script for openwebui to use grok-2-image, which modifies the request by listening and forwarding to support the format of grok-2-image. > > > > > In fact, I asked AI to help me do this. Currently, there is no problem in using it under normal circumstances. You only need to fill in the address and port of openwebui's AI drawing as the address and port of pyhon running. > > > > > ``` > > > > > import http.server > > > > > import socketserver > > > > > import requests > > > > > import json > > > > > import time > > > > > > > > > > class RequestHandler(http.server.SimpleHTTPRequestHandler): > > > > > def do_POST(self): > > > > > # 读取请求内容 > > > > > content_length = int(self.headers.get('Content-Length', 0)) > > > > > request_body = self.rfile.read(content_length) > > > > > > > > > > # 解析请求体 > > > > > try: > > > > > request_data = json.loads(request_body.decode('utf-8')) > > > > > except json.JSONDecodeError: > > > > > print("Error: Invalid JSON in request body") > > > > > self.send_error(400, "Invalid JSON in request body") > > > > > return > > > > > > > > > > # 修改请求数据 > > > > > request_data['model'] = "grok-2-image" > > > > > request_data['response_format'] = "b64_json" > > > > > > > > > > # 删除size参数 > > > > > request_data.pop('size', None) > > > > > > > > > > target_url = "https://api.x.ai/v1/images/generations" > > > > > headers = { > > > > > "Authorization": self.headers.get('Authorization', ''), > > > > > "Content-Type": "application/json" > > > > > } > > > > > > > > > > try: > > > > > response = requests.post( > > > > > target_url, > > > > > headers=headers, > > > > > data=json.dumps(request_data) > > > > > ) > > > > > > > > > > # 解析原始响应 > > > > > original_response = response.json() > > > > > > > > > > # 修改响应格式,仅添加created字段 > > > > > modified_response = original_response.copy() > > > > > modified_response['created'] = int(time.time()) > > > > > > > > > > # 发送修改后的响应回给原始请求者 > > > > > self.send_response(response.status_code) > > > > > self.send_header("Content-Type", "application/json") > > > > > self.end_headers() > > > > > self.wfile.write(json.dumps(modified_response).encode('utf-8')) > > > > > > > > > > except requests.RequestException as e: > > > > > print(f"\nError forwarding request: {e}") > > > > > self.send_error(500, "Error forwarding request") > > > > > except (KeyError, IndexError, json.JSONDecodeError) as e: > > > > > print(f"\nError processing response: {e}") > > > > > self.send_error(500, "Error processing response") > > > > > > > > > > def log_message(self, format, *args): > > > > > # 禁用默认的日志输出 > > > > > return > > > > > > > > > > # 设置服务器地址和端口 > > > > > PORT = 9988 > > > > > Handler = RequestHandler > > > > > > > > > > # 创建并运行服务器 > > > > > with socketserver.TCPServer(("", PORT), Handler) as httpd: > > > > > print(f"Serving at port {PORT}") > > > > > httpd.serve_forever() > > > > > ``` > > > > > > > > > > > > Hello, what option does "the address and port of openwebui's AI drawing" refer to? I tried running on port 9988 and it still shows "OpenAI: Network Problem". There is no problem with my network environment. > > > > > > > > > Set it in the OPENAI API in the image generation settings, for example: http://192.168.10.11:9988/v1, this address is the server address running the python script,and fill in your own grok api for the secret key > > > > > > I have tried changing this option, including the local address and http://localhost:9988/v1 as you said. My problem still occurs. So how do you set up your Grok API? Is it All endpoints and All models or just images? > > Please make sure that your openwebui can connect to the address where python is running. If the openwebui is deployed by docker, you should confirm again whether the network can connect to the address where python is running. My API uses all the models. Thanks for your help. I also realized the generation of multiple images. The following is based on your code modification. ``` import http.server import socketserver import requests import json import time import re # 配置 TARGET_URL = "https://api.x.ai/v1/images/generations" PORT = 9988 # 本地监听端口 class ProxyHandler(http.server.BaseHTTPRequestHandler): def do_POST(self): # 读取 Open WebUI 发来的数据 content_length = int(self.headers.get('Content-Length', 0)) post_data = self.rfile.read(content_length) try: request_json = json.loads(post_data.decode('utf-8')) except json.JSONDecodeError: self.send_error(400, "Invalid JSON") return # 提取提示词 prompt = request_json.get("messages", [{}])[-1].get("content", "") if not prompt: self.send_error(400, "No prompt found") return # 从提示词中提取图像数量 match = re.search(r'(\d+)\s*张', prompt) if match: n = int(match.group(1)) n = max(1, min(n, 10)) # 限制 n 的范围在 1 到 10 else: n = 1 # 默认生成一张图像 # 构建新的请求数据 new_payload = { "model": "grok-2-image", "prompt": prompt, "response_format": "b64_json", "n": n } headers = { "Authorization": self.headers.get('Authorization', ''), "Content-Type": "application/json" } # 发送请求到 xAI 图像生成接口 try: response = requests.post( TARGET_URL, headers=headers, json=new_payload, timeout=60 ) response.raise_for_status() res_json = response.json() # 构建返回给 Open WebUI 的响应数据 images = res_json.get('data', []) if not images: self.send_error(502, "No image data received from xAI") return # 将所有图片合成一条Markdown消息 image_markdown = "" for idx, image in enumerate(images): b64_image = image.get('b64_json', '') if b64_image: image_markdown += f"![Image {idx+1}](data:image/png;base64,{b64_image})\n" modified_response = { "id": "fake-id", "object": "chat.completion", "created": int(time.time()), "choices": [{ "index": 0, "message": { "role": "assistant", "content": image_markdown.strip() }, "finish_reason": "stop" }], "usage": { "prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0 } } self.send_response(200) self.send_header('Content-Type', 'application/json') self.end_headers() self.wfile.write(json.dumps(modified_response).encode('utf-8')) except requests.RequestException as e: self.send_error(502, f"Error contacting xAI: {e}") def do_GET(self): if self.path == "/v1/models": models = { "object": "list", "data": [ { "id": "grok-2-image", "object": "model", "created": int(time.time()), "owned_by": "openai", "permission": [] } ] } self.send_response(200) self.send_header("Content-Type", "application/json") self.end_headers() self.wfile.write(json.dumps(models).encode('utf-8')) else: self.send_error(404, "Not Found") def log_message(self, format, *args): return # 启动服务器 with socketserver.TCPServer(("", PORT), ProxyHandler) as httpd: print(f"✅ Proxy Server running at http://localhost:{PORT}") httpd.serve_forever() ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#31934