Repository Public
1#!/usr/bin/env python32# =============================================================================3# SP BOUCHER | SOFTWARE & RESEARCH4# ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5# =============================================================================6#7# PROJECT METADATA8# -----------------------------------------------------------------------------9# Project : API Atlas10# File : examples/xai/images/generate.py11# Description : Generate one image with the OpenAI Python SDK pointed at12# xAI (client.images.generate).13# Module : examples14# Repository : https://git.spboucher.ai/doc-api15# Created : 2026-09-1916# Last Modified : 2026-09-1917#18# =============================================================================19# AUTHOR & PROFESSIONAL AFFILIATION20# =============================================================================21#22# Author : Simon-Pierre Boucher23# Position : Professor of Finance24# Institution : Université du Québec en Outaouais (UQO)25# Department : Département des sciences administratives26# Organization : SP Boucher27#28# Website : https://spboucher.ai29# Git : https://git.spboucher.ai30#31# =============================================================================32# OFFICIAL CONTACT DIRECTORY33# =============================================================================34#35# General : info@spboucher.ai36# Professional : contact@spboucher.ai37# Direct : simon@spboucher.ai38# Research : research@spboucher.ai39# Development : dev@spboucher.ai40# Support : support@spboucher.ai41# Security/Admin : admin@spboucher.ai42# Notifications : notifications@spboucher.ai43#44# Preferred Contact: contact@spboucher.ai45#46# =============================================================================47# COPYRIGHT & INTELLECTUAL PROPERTY48# =============================================================================49#50# Copyright (c) 202651# Simon-Pierre Boucher52#53# All rights reserved where applicable.54#55# This file is governed by the applicable project license,56# copyright notices, institutional agreements and relevant law.57#58# Unless expressly authorized by the applicable license or59# rights holder, copying, redistribution, sublicensing,60# publication and commercial exploitation are not permitted.61#62# Existing third-party rights and notices remain unaffected.63#64# =============================================================================65# CONFIDENTIALITY & SECURITY66# =============================================================================67#68# Classification : Public69# Distribution : Public repository; use is governed by the project license70# Security Contact : admin@spboucher.ai71#72# Access to this source does not independently grant permission73# to redistribute or disclose confidential materials.74#75# Do not embed credentials, private keys, access tokens,76# certificates or confidential personal information.77#78# =============================================================================79# LICENSE & MAINTENANCE80# =============================================================================81#82# License : None specified (no license file in this repository)83# Maintainer : Simon-Pierre Boucher84# Technical Contact: dev@spboucher.ai85#86# =============================================================================87# END OF SP BOUCHER SOURCE HEADER88# =============================================================================8990"""Generate one image with the OpenAI Python SDK pointed at xAI (client.images.generate).9192STATUS: UNVERIFIED as a script (the identical raw request was LIVE_VERIFIED 2026-09-18 via scripts/live.py:93 grok-imagine-image, 1:1, 1k, b64_json -> 200, JPEG, $0.02). Not run automatically (paid).94xAI-specific fields (aspect_ratio, resolution, quality) go through `extra_body`; do NOT pass `size`/`quality` OpenAI-style.95Run: .venv/bin/python examples/xai/images/generate.py "a plain white square"96"""97from __future__ import annotations9899import base64100import os101import sys102from pathlib import Path103104ROOT = Path(__file__).resolve().parents[3]105sys.path.insert(0, str(ROOT))106from scripts import live # noqa: E402 loads .env, gives log_request107108from openai import OpenAI # noqa: E402109110client = OpenAI(api_key=os.environ["XAI_API_KEY"], base_url="https://api.x.ai/v1")111prompt = sys.argv[1] if len(sys.argv) > 1 else "a plain white square"112resp = client.images.generate(113 model="grok-imagine-image", # $0.02/image; grok-imagine-image-2.0 adds quality low|medium|auto114 prompt=prompt,115 n=1,116 response_format="b64_json",117 extra_body={"aspect_ratio": "1:1", "resolution": "1k"},118)119live.log_request("xai", "POST", "/v1/images/generations", 200, 0.02, "example images/generate.py openai-sdk")120img = resp.data[0]121out = ROOT / "tmp-live" / "xai-example-generate.jpg"122out.write_bytes(base64.b64decode(img.b64_json))123extra = resp.model_extra or {}124print({"mime_type": getattr(img, "mime_type", None) or (img.model_extra or {}).get("mime_type"), "usage": extra.get("usage")})125print("saved", out)126