Generate images from text prompts through TrueFoundry’s AI Gateway
gpt-image-1
dall-e-2
dall-e-3
Amazon Nova Canvas models
Stability AI models
from openai import OpenAI import base64 BASE_URL = "https://{controlPlaneUrl}/api/llm" API_KEY = "your-truefoundry-api-key" # Configure OpenAI client with TrueFoundry settings client = OpenAI( api_key=API_KEY, base_url=BASE_URL, ) try: response = client.images.generate( model="openai-main/dalle-e-2", prompt="A beautiful sunset over mountains", n=1, size="1024x1024" ) # Handle URL response if hasattr(response.data[0], 'url') and response.data[0].url: print(f"Image URL: {response.data[0].url}") # Handle base64 response if hasattr(response.data[0], 'b64_json') and response.data[0].b64_json: image_bytes = base64.b64decode(response.data[0].b64_json) with open("generated_image.png", "wb") as f: f.write(image_bytes) print("Image saved as generated_image.png") except Exception as e: print(f"Error: {e}")
{ "created": 1713833628, "data": [ { "url": "...", "b64_json": "..." } ] }
Was this page helpful?