Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/brown-drinks-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ai-sdk/gateway': patch
---

feat(provider/gateway): Server-side image request splitting
18 changes: 1 addition & 17 deletions packages/gateway/src/gateway-image-model-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,4 @@ export type GatewayImageModelId =
| 'bfl/flux-pro-1.1'
| 'bfl/flux-pro-1.1-ultra'
| (string & {});

/**
Default number of images a provider can return per single request.
Only include providers that deviate from the default of 4 (e.g. Vertex
Imagen is 4).
*/
export const DEFAULT_MAX_IMAGES_PER_CALL_BY_PROVIDER: Record<string, number> = {
bfl: 1,
};

export function getDefaultMaxImagesPerCallForModel(
modelId: GatewayImageModelId,
): number | undefined {
const providerPrefix =
typeof modelId === 'string' ? modelId.split('/')[0] : '';
return DEFAULT_MAX_IMAGES_PER_CALL_BY_PROVIDER[providerPrefix];
}

6 changes: 3 additions & 3 deletions packages/gateway/src/gateway-image-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ describe('GatewayImageModel', () => {
expect(model.modelId).toBe(TEST_MODEL_ID);
expect(model.provider).toBe('gateway');
expect(model.specificationVersion).toBe('v3');
expect(model.maxImagesPerCall).toBe(4);
expect(model.maxImagesPerCall).toBe(Number.MAX_SAFE_INTEGER);
});

it('should use provider default maxImagesPerCall for BFL models (1)', () => {
it('should avoid client-side splitting even for BFL models', () => {
const model = new GatewayImageModel('bfl/flux-pro-1.1', {
provider: 'gateway',
baseURL: 'https://api.test.com',
Expand All @@ -47,7 +47,7 @@ describe('GatewayImageModel', () => {
o11yHeaders: {},
});

expect(model.maxImagesPerCall).toBe(1);
expect(model.maxImagesPerCall).toBe(Number.MAX_SAFE_INTEGER);
});

it('should accept custom provider name', () => {
Expand Down
6 changes: 2 additions & 4 deletions packages/gateway/src/gateway-image-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ import { z } from 'zod/v4';
import type { GatewayConfig } from './gateway-config';
import { asGatewayError } from './errors';
import { parseAuthMethod } from './errors/parse-auth-method';
import { getDefaultMaxImagesPerCallForModel } from './gateway-image-model-settings';

export class GatewayImageModel implements ImageModelV3 {
readonly specificationVersion = 'v3' as const;
get maxImagesPerCall() {
return getDefaultMaxImagesPerCallForModel(this.modelId) ?? 4;
}
// Set a very large number to prevent client-side splitting of requests
readonly maxImagesPerCall = Number.MAX_SAFE_INTEGER;

constructor(
readonly modelId: string,
Expand Down
Loading