diff --git a/types/defines/ai.d.ts b/types/defines/ai.d.ts index c8fb8437c0b..f1d2547b201 100644 --- a/types/defines/ai.d.ts +++ b/types/defines/ai.d.ts @@ -632,12 +632,11 @@ export type ChatCompletionChoice = { finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | "function_call"; logprobs: ChatCompletionLogprobs | null; }; -export type ChatCompletionsPromptInput = { - prompt: string; -} & ChatCompletionsCommonOptions; + export type ChatCompletionsMessagesInput = { messages: Array; } & ChatCompletionsCommonOptions; + export type ChatCompletionsOutput = { id: string; object: string; @@ -648,6 +647,7 @@ export type ChatCompletionsOutput = { system_fingerprint?: string | null; service_tier?: "auto" | "default" | "flex" | "scale" | "priority" | null; }; + /** * Workers AI support for OpenAI's Responses API * Reference: https://github.com/openai/openai-node/blob/master/src/resources/responses/responses.ts @@ -4730,11 +4730,11 @@ export declare abstract class Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2 { postProcessedOutputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output; } export declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_120B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } export declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_20B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } export interface Ai_Cf_Leonardo_Phoenix_1_0_Input { @@ -5824,7 +5824,7 @@ export declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_5 { postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_6 { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { @@ -5832,9 +5832,10 @@ export declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Google_Gemma_4_26B_A4B_IT { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } + export interface AiModels { "@cf/huggingface/distilbert-sst-2-int8": BaseAiTextClassification; "@cf/stabilityai/stable-diffusion-xl-base-1.0": BaseAiTextToImage; @@ -5979,13 +5980,10 @@ export type AiModelsSearchObject = { value: string; }[]; }; -export type ChatCompletionsBase = XOR; -export type ChatCompletionsInput = XOR< - ChatCompletionsBase, - { - requests: ChatCompletionsBase[]; - } ->; + +export type ChatCompletionsBase = ChatCompletionsMessagesInput; +export type ChatCompletionsInput = ChatCompletionsMessagesInput; + export interface InferenceUpstreamError extends Error {} export interface AiInternalError extends Error {} export type AiModelListType = Record; @@ -6046,9 +6044,16 @@ export declare abstract class Ai< options?: AiOptions ): Promise; - // Unknown model (gateway fallback) - run( - model: string & {}, + // Unknown model (fallback). + // + // The `Exclude<..., keyof AiModelList>` constraint forces TypeScript to + // route any model name that is a literal key of `AiModelList` to one of + // the known-model overloads above (so input/output mismatches surface as + // type errors rather than silently falling back to `Record`). + // Names that aren't in `AiModelList` — e.g. third-party gateway models + // like `"google/nano-banana"` — still hit this overload. + run( + model: Model extends keyof AiModelList ? never : Model, inputs: Record, options?: AiOptions ): Promise>; diff --git a/types/generated-snapshot/experimental/index.d.ts b/types/generated-snapshot/experimental/index.d.ts index 85f9cb25e56..cec7eee584d 100755 --- a/types/generated-snapshot/experimental/index.d.ts +++ b/types/generated-snapshot/experimental/index.d.ts @@ -6177,9 +6177,6 @@ type ChatCompletionChoice = { | "function_call"; logprobs: ChatCompletionLogprobs | null; }; -type ChatCompletionsPromptInput = { - prompt: string; -} & ChatCompletionsCommonOptions; type ChatCompletionsMessagesInput = { messages: Array; } & ChatCompletionsCommonOptions; @@ -10304,11 +10301,11 @@ declare abstract class Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2 { postProcessedOutputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output; } declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_120B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_20B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } interface Ai_Cf_Leonardo_Phoenix_1_0_Input { @@ -11403,7 +11400,7 @@ declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_5 { postProcessedOutputs: ChatCompletionsOutput; } declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_6 { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { @@ -11411,7 +11408,7 @@ declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { postProcessedOutputs: ChatCompletionsOutput; } declare abstract class Base_Ai_Cf_Google_Gemma_4_26B_A4B_IT { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } interface AiModels { @@ -11558,16 +11555,8 @@ type AiModelsSearchObject = { value: string; }[]; }; -type ChatCompletionsBase = XOR< - ChatCompletionsPromptInput, - ChatCompletionsMessagesInput ->; -type ChatCompletionsInput = XOR< - ChatCompletionsBase, - { - requests: ChatCompletionsBase[]; - } ->; +type ChatCompletionsBase = ChatCompletionsMessagesInput; +type ChatCompletionsInput = ChatCompletionsMessagesInput; interface InferenceUpstreamError extends Error {} interface AiInternalError extends Error {} type AiModelListType = Record; @@ -11630,9 +11619,16 @@ declare abstract class Ai { inputs: AiModelList[Name]["inputs"], options?: AiOptions, ): Promise; - // Unknown model (gateway fallback) - run( - model: string & {}, + // Unknown model (fallback). + // + // The `Exclude<..., keyof AiModelList>` constraint forces TypeScript to + // route any model name that is a literal key of `AiModelList` to one of + // the known-model overloads above (so input/output mismatches surface as + // type errors rather than silently falling back to `Record`). + // Names that aren't in `AiModelList` — e.g. third-party gateway models + // like `"google/nano-banana"` — still hit this overload. + run( + model: Model extends keyof AiModelList ? never : Model, inputs: Record, options?: AiOptions, ): Promise>; diff --git a/types/generated-snapshot/experimental/index.ts b/types/generated-snapshot/experimental/index.ts index d8afd32e79c..5f1fae81a1e 100755 --- a/types/generated-snapshot/experimental/index.ts +++ b/types/generated-snapshot/experimental/index.ts @@ -6186,9 +6186,6 @@ export type ChatCompletionChoice = { | "function_call"; logprobs: ChatCompletionLogprobs | null; }; -export type ChatCompletionsPromptInput = { - prompt: string; -} & ChatCompletionsCommonOptions; export type ChatCompletionsMessagesInput = { messages: Array; } & ChatCompletionsCommonOptions; @@ -10314,11 +10311,11 @@ export declare abstract class Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2 { postProcessedOutputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output; } export declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_120B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } export declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_20B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } export interface Ai_Cf_Leonardo_Phoenix_1_0_Input { @@ -11413,7 +11410,7 @@ export declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_5 { postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_6 { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { @@ -11421,7 +11418,7 @@ export declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Google_Gemma_4_26B_A4B_IT { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } export interface AiModels { @@ -11568,16 +11565,8 @@ export type AiModelsSearchObject = { value: string; }[]; }; -export type ChatCompletionsBase = XOR< - ChatCompletionsPromptInput, - ChatCompletionsMessagesInput ->; -export type ChatCompletionsInput = XOR< - ChatCompletionsBase, - { - requests: ChatCompletionsBase[]; - } ->; +export type ChatCompletionsBase = ChatCompletionsMessagesInput; +export type ChatCompletionsInput = ChatCompletionsMessagesInput; export interface InferenceUpstreamError extends Error {} export interface AiInternalError extends Error {} export type AiModelListType = Record; @@ -11642,9 +11631,16 @@ export declare abstract class Ai< inputs: AiModelList[Name]["inputs"], options?: AiOptions, ): Promise; - // Unknown model (gateway fallback) - run( - model: string & {}, + // Unknown model (fallback). + // + // The `Exclude<..., keyof AiModelList>` constraint forces TypeScript to + // route any model name that is a literal key of `AiModelList` to one of + // the known-model overloads above (so input/output mismatches surface as + // type errors rather than silently falling back to `Record`). + // Names that aren't in `AiModelList` — e.g. third-party gateway models + // like `"google/nano-banana"` — still hit this overload. + run( + model: Model extends keyof AiModelList ? never : Model, inputs: Record, options?: AiOptions, ): Promise>; diff --git a/types/generated-snapshot/latest/index.d.ts b/types/generated-snapshot/latest/index.d.ts index 9dd7f83b0da..89be26c5556 100755 --- a/types/generated-snapshot/latest/index.d.ts +++ b/types/generated-snapshot/latest/index.d.ts @@ -5509,9 +5509,6 @@ type ChatCompletionChoice = { | "function_call"; logprobs: ChatCompletionLogprobs | null; }; -type ChatCompletionsPromptInput = { - prompt: string; -} & ChatCompletionsCommonOptions; type ChatCompletionsMessagesInput = { messages: Array; } & ChatCompletionsCommonOptions; @@ -9636,11 +9633,11 @@ declare abstract class Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2 { postProcessedOutputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output; } declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_120B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_20B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } interface Ai_Cf_Leonardo_Phoenix_1_0_Input { @@ -10735,7 +10732,7 @@ declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_5 { postProcessedOutputs: ChatCompletionsOutput; } declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_6 { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { @@ -10743,7 +10740,7 @@ declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { postProcessedOutputs: ChatCompletionsOutput; } declare abstract class Base_Ai_Cf_Google_Gemma_4_26B_A4B_IT { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } interface AiModels { @@ -10890,16 +10887,8 @@ type AiModelsSearchObject = { value: string; }[]; }; -type ChatCompletionsBase = XOR< - ChatCompletionsPromptInput, - ChatCompletionsMessagesInput ->; -type ChatCompletionsInput = XOR< - ChatCompletionsBase, - { - requests: ChatCompletionsBase[]; - } ->; +type ChatCompletionsBase = ChatCompletionsMessagesInput; +type ChatCompletionsInput = ChatCompletionsMessagesInput; interface InferenceUpstreamError extends Error {} interface AiInternalError extends Error {} type AiModelListType = Record; @@ -10962,9 +10951,16 @@ declare abstract class Ai { inputs: AiModelList[Name]["inputs"], options?: AiOptions, ): Promise; - // Unknown model (gateway fallback) - run( - model: string & {}, + // Unknown model (fallback). + // + // The `Exclude<..., keyof AiModelList>` constraint forces TypeScript to + // route any model name that is a literal key of `AiModelList` to one of + // the known-model overloads above (so input/output mismatches surface as + // type errors rather than silently falling back to `Record`). + // Names that aren't in `AiModelList` — e.g. third-party gateway models + // like `"google/nano-banana"` — still hit this overload. + run( + model: Model extends keyof AiModelList ? never : Model, inputs: Record, options?: AiOptions, ): Promise>; diff --git a/types/generated-snapshot/latest/index.ts b/types/generated-snapshot/latest/index.ts index 4fdac7047be..4c2fdb200d5 100755 --- a/types/generated-snapshot/latest/index.ts +++ b/types/generated-snapshot/latest/index.ts @@ -5518,9 +5518,6 @@ export type ChatCompletionChoice = { | "function_call"; logprobs: ChatCompletionLogprobs | null; }; -export type ChatCompletionsPromptInput = { - prompt: string; -} & ChatCompletionsCommonOptions; export type ChatCompletionsMessagesInput = { messages: Array; } & ChatCompletionsCommonOptions; @@ -9646,11 +9643,11 @@ export declare abstract class Base_Ai_Cf_Pipecat_Ai_Smart_Turn_V2 { postProcessedOutputs: Ai_Cf_Pipecat_Ai_Smart_Turn_V2_Output; } export declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_120B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } export declare abstract class Base_Ai_Cf_Openai_Gpt_Oss_20B { - inputs: XOR; + inputs: XOR; postProcessedOutputs: XOR; } export interface Ai_Cf_Leonardo_Phoenix_1_0_Input { @@ -10745,7 +10742,7 @@ export declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_5 { postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Moonshotai_Kimi_K2_6 { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { @@ -10753,7 +10750,7 @@ export declare abstract class Base_Ai_Cf_Nvidia_Nemotron_3_120B_A12B { postProcessedOutputs: ChatCompletionsOutput; } export declare abstract class Base_Ai_Cf_Google_Gemma_4_26B_A4B_IT { - inputs: ChatCompletionsBase; + inputs: ChatCompletionsInput; postProcessedOutputs: ChatCompletionsOutput; } export interface AiModels { @@ -10900,16 +10897,8 @@ export type AiModelsSearchObject = { value: string; }[]; }; -export type ChatCompletionsBase = XOR< - ChatCompletionsPromptInput, - ChatCompletionsMessagesInput ->; -export type ChatCompletionsInput = XOR< - ChatCompletionsBase, - { - requests: ChatCompletionsBase[]; - } ->; +export type ChatCompletionsBase = ChatCompletionsMessagesInput; +export type ChatCompletionsInput = ChatCompletionsMessagesInput; export interface InferenceUpstreamError extends Error {} export interface AiInternalError extends Error {} export type AiModelListType = Record; @@ -10974,9 +10963,16 @@ export declare abstract class Ai< inputs: AiModelList[Name]["inputs"], options?: AiOptions, ): Promise; - // Unknown model (gateway fallback) - run( - model: string & {}, + // Unknown model (fallback). + // + // The `Exclude<..., keyof AiModelList>` constraint forces TypeScript to + // route any model name that is a literal key of `AiModelList` to one of + // the known-model overloads above (so input/output mismatches surface as + // type errors rather than silently falling back to `Record`). + // Names that aren't in `AiModelList` — e.g. third-party gateway models + // like `"google/nano-banana"` — still hit this overload. + run( + model: Model extends keyof AiModelList ? never : Model, inputs: Record, options?: AiOptions, ): Promise>; diff --git a/types/test/types/ai.ts b/types/test/types/ai.ts index a8ad0ff7bb3..6eab9658521 100644 --- a/types/test/types/ai.ts +++ b/types/test/types/ai.ts @@ -4,6 +4,7 @@ function expectType(_value: T) {} + export const handler: ExportedHandler<{ AI: Ai }> = { async fetch(_request, env) { // Known model -- normal response @@ -62,6 +63,19 @@ export const handler: ExportedHandler<{ AI: Ai }> = { expectType>(result); } + // Known model names do not silently fall through to the unknown-model + // gateway-fallback overload. The fallback's signature excludes + // `keyof AiModelList`, so a call with a known model name and an input + // shape that doesn't match the known-model overload must surface as a + // type error rather than degrading to `Record`. Here + // we exercise that with kimi-k2.6, whose typed input is + // `ChatCompletionsMessagesInput` (no `prompt` field). + { + // @ts-expect-error: kimi-k2.6 takes `messages`, not `prompt`; the + // unknown-model fallback must not catch this call. + await env.AI.run('@cf/moonshotai/kimi-k2.6', { prompt: 'hello' }); + } + return new Response(); }, };