Skip to content

Commit 4820258

Browse files
committed
automatically ignore ResponseFormat for gpt-image models
1 parent 43967ed commit 4820258

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/LlmTornado/Images/ImageGenerationRequest.cs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,31 @@ internal void OverrideUrl(string url)
221221
UrlOverride = url;
222222
}
223223

224+
internal ImageGenerationRequest(ImageGenerationRequest basedOn)
225+
{
226+
CopyData(basedOn);
227+
}
228+
229+
private void CopyData(ImageGenerationRequest basedOn)
230+
{
231+
Prompt = basedOn.Prompt;
232+
NumOfImages = basedOn.NumOfImages;
233+
User = basedOn.User;
234+
Background = basedOn.Background;
235+
OutputFormat = basedOn.OutputFormat;
236+
Moderation = basedOn.Moderation;
237+
OutputCompression = basedOn.OutputCompression;
238+
Size = basedOn.Size;
239+
Width = basedOn.Width;
240+
Height = basedOn.Height;
241+
ResponseFormat = basedOn.ResponseFormat;
242+
Model = basedOn.Model;
243+
Quality = basedOn.Quality;
244+
Style = basedOn.Style;
245+
VendorExtensions = basedOn.VendorExtensions;
246+
UrlOverride = basedOn.UrlOverride;
247+
}
248+
224249
/// <summary>
225250
/// Serializes the chat request into the request body, based on the conventions used by the LLM provider.
226251
/// </summary>
@@ -231,9 +256,24 @@ public TornadoRequestContent Serialize(IEndpointProvider provider)
231256
return SerializeMap.TryGetValue(provider.Provider, out Func<ImageGenerationRequest, IEndpointProvider, string>? serializerFn) ? new TornadoRequestContent(serializerFn.Invoke(this, provider), Model, UrlOverride, provider, CapabilityEndpoints.ImageGeneration) : new TornadoRequestContent(string.Empty, Model, UrlOverride, provider, CapabilityEndpoints.ImageGeneration);
232257
}
233258

259+
private static bool IsGptImageModel(ImageModel? model)
260+
{
261+
string? name = model?.GetApiName;
262+
return name is not null && (name.StartsWith("gpt-image", StringComparison.OrdinalIgnoreCase) || name.StartsWith("chatgpt-image", StringComparison.OrdinalIgnoreCase));
263+
}
264+
234265
private static readonly FrozenDictionary<LLmProviders, Func<ImageGenerationRequest, IEndpointProvider, string>> SerializeMap = new Dictionary<LLmProviders, Func<ImageGenerationRequest, IEndpointProvider, string>>
235266
{
236-
{ LLmProviders.OpenAi, (x, y) => JsonConvert.SerializeObject(x, EndpointBase.NullSettings)},
267+
{ LLmProviders.OpenAi, (x, y) =>
268+
{
269+
if (IsGptImageModel(x.Model) && x.ResponseFormat is not null)
270+
{
271+
x = new ImageGenerationRequest(x);
272+
x.ResponseFormat = null;
273+
}
274+
275+
return JsonConvert.SerializeObject(x, EndpointBase.NullSettings);
276+
}},
237277
{ LLmProviders.XAi, (x, y) => JsonConvert.SerializeObject(new VendorXAiImageRequest(x, y), EndpointBase.NullSettings) },
238278
{ LLmProviders.Google, (x, y) => JsonConvert.SerializeObject(new VendorGoogleImageRequest(x, y), EndpointBase.NullSettings) },
239279
{ LLmProviders.MiniMax, (x, y) => JsonConvert.SerializeObject(new VendorMiniMaxImageRequest(x, y), EndpointBase.NullSettings) }

0 commit comments

Comments
 (0)