Fix image generation using FLUX.2 via AI Gateway#23
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| varName: string | ||
| ): string[] { | ||
| imports.add("import { generateImage } from './integrations/ai';"); | ||
| imports.add( |
There was a problem hiding this comment.
The generateAiImageActionCode function generates incomplete code that's missing the required API key configuration. This will cause runtime failures when the generated code tries to call the generateImage function without authentication.
View Details
📝 Patch Details
diff --git a/lib/workflow-codegen.ts b/lib/workflow-codegen.ts
index 9d74a99..4472d59 100644
--- a/lib/workflow-codegen.ts
+++ b/lib/workflow-codegen.ts
@@ -452,6 +452,11 @@ export function generateWorkflowCode(
`${indent} model: "${imageModel}",`,
`${indent} prompt: \`${imagePrompt}\`,`,
`${indent} size: "1024x1024",`,
+ `${indent} providerOptions: {`,
+ `${indent} openai: {`,
+ `${indent} apiKey: process.env.AI_GATEWAY_API_KEY!`,
+ `${indent} },`,
+ `${indent} },`,
`${indent}});`,
];
}
Analysis
Missing API key configuration in generateAiImageActionCode
What fails: generateAiImageActionCode() in lib/workflow-codegen.ts (lines 436-457) generates code that calls experimental_generateImage without the required providerOptions.openai.apiKey parameter, causing authentication failures when the generated code is executed.
How to reproduce:
- Create a workflow with a "Generate Image" action
- Download/export the generated code via the API endpoint
- Execute the generated code without setting
OPENAI_API_KEYenvironment variable - The
generateImage()call fails with authentication error
Result: Generated code produces authentication error:
Error: OpenAI API request failed: 401 Unauthorized - Incorrect API key provided
Expected: According to Vercel AI SDK documentation, providerOptions parameter can be used to pass provider-specific settings including API keys. The generated code should match the pattern used in:
lib/codegen-templates/generate-image.ts(lines 17-21) - template version includesproviderOptionslib/workflow-codegen-sdk.ts(line 514) - SDK version includesproviderOptionslib/steps/generate-image.ts(lines 36-40) - step function version includesproviderOptions
All three existing patterns consistently pass providerOptions: { openai: { apiKey: process.env.AI_GATEWAY_API_KEY } } to ensure authentication works correctly.
Fix: Added providerOptions parameter to the generated code in generateAiImageActionCode() to match the existing patterns used throughout the codebase.
* Fix image generation by using gpt-image-1 via ai gateway * Update model to bfl/flux-2-pro * Remove dalle 2 and 3 support * Remove flash image and its preview * Replace open api key with ai gateway api key * Add example usage of experimental_generateImage for 1024x1024 image --------- Co-authored-by: Chris Tate <ctate@users.noreply.github.com>
No description provided.