fix: make model option types match what OpenAI and Anthropic accept #2242
tanstack-source requested a review fromjherr
tanstack-source requested a review fromTkDodo
- @@ -9,8 +9,9 @@991010 ```diff1111 chat({12- adapter: openaiText('gpt-6.1-sol'),12+ adapter: openaiText('gpt-6.1-terra'),1313 messages,1414 temperature: 0.3,1515 topP: 0.9,1616 maxTokens: 100,17+ reasoning: 'off',@@ -83,7 +83,14 @@8383 ? OpenAIModelInputModalitiesByName[TModel]8484 : readonly ['text', 'image', 'audio']858586-/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */86+/**87+ * The reasoning levels of a model, for `chat({ reasoning })`. `never`: none.88+ * A reasoning model takes the sampling options only with `reasoning: 'off'`,89+ * the same rule as `openAIModelRejectsSamplingParams`.90+ */8791 type ResolveReasoning<TModel extends string> =8892 TModel extends keyof OpenAIModelReasoningByName89- ? OpenAIModelReasoningByName[TModel]93+ ? TModel extends `${string}-chat-latest`94+ ? OpenAIModelReasoningByName[TModel]95+ : OpenAIModelReasoningByName[TModel] & {96+ offOnlyOptions: 'temperature' | 'top_p' | 'top_logprobs'@@ -0,0 +88,21 @@88+ it('finds the models of the catalog', () => {89+ expect(models.length).toBeGreaterThan(10)90+ })91+92+ it('has reasoning data for each model that OpenRouter says reasons', () => {93+ const missing = models94+ .filter((meta) => meta.params.includes('reasoning_effort'))95+ .filter((meta) => !/\n {2}reasoning: /.test(meta.source))96+ .map((meta) => meta.model)97+ .filter((model) => !EXCEPTIONS[`${config.provider}:${model}:reasoning`])98+ expect(missing).toEqual([])99+ })100+101+ it.runIf(config.provider === 'anthropic')(102+ 'has sampling options only for models that OpenRouter says take them',103+ () => {104+ const wrong = models.flatMap((meta) => {105+ if (EXCEPTIONS[`anthropic:${meta.model}:sampling`]) return []106+ const expected = ['temperature', 'top_p', 'top_k'].some((param) =>107+ meta.params.includes(param),108+ )
tanstack-source reviewed
@@ -0,0 +20,21 @@20+ readFileSync(resolve(ROOT, 'scripts/openrouter.models.json'), 'utf8'),21+)22+23+/**24+ * Models where OpenRouter is wrong. Each entry needs a reason.25+ * Keys are `<provider>:<model>:<rule>`.26+ */27+const EXCEPTIONS: Record<string, string> = {}28+29+interface MetaModel {30+ constName: string31+ model: string32+ /** The constant body, from `{` to `} as const`, plus its `satisfies` type. */33+ source: string34+}35+36+/** Each `const X = { name|id: '...' ... } as const satisfies ModelMeta<...>`. */37+function readModels(file: string): Array<MetaModel> {38+ const content = readFileSync(resolve(ROOT, file), 'utf8')39+ const re =40+ /^const (\w+) = \{\n {2}(?:name|id): '([^']+)'[\s\S]*?^\} as const satisfies ModelMeta<[\s\S]*?\n(?=\n|$)/gm
Applied a fix from CI111a96b
- @@ -0,0 +20,21 @@20+ readFileSync(resolve(ROOT, 'scripts/openrouter.models.json'), 'utf8'),21+)22+23+/**24+ * Models where OpenRouter is wrong. Each entry needs a reason.25+ * Keys are `<provider>:<model>:<rule>`.26+ */27+const EXCEPTIONS: Record<string, string> = {}28+29+interface MetaModel {30+ constName: string31+ model: string32+ /** The constant body, from `{` to `} as const`, plus its `satisfies` type. */33+ source: string34+}35+36+/** Each `const X = { name|id: '...' ... } as const satisfies ModelMeta<...>`. */37+function readModels(file: string): Array<MetaModel> {38+ const content = readFileSync(resolve(ROOT, file), 'utf8')39+ const re =40+ /^const (\w+) = \{\n {2}(?:name|id): '([^']+)'[\s\S]*?^\} as const satisfies ModelMeta<[^>]*>/gm
tanstack-source reviewed
@@ -83,7 +83,14 @@8383 ? OpenAIModelInputModalitiesByName[TModel]8484 : readonly ['text', 'image', 'audio']858586-/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */86+/**87+ * The reasoning levels of a model, for `chat({ reasoning })`. `never`: none.88+ * A reasoning model takes the sampling options only with `reasoning: 'off'`,89+ * the same rule as `openAIModelRejectsSamplingParams`.90+ */8791 type ResolveReasoning<TModel extends string> =8892 TModel extends keyof OpenAIModelReasoningByName89- ? OpenAIModelReasoningByName[TModel]93+ ? TModel extends `${string}-chat-latest`94+ ? OpenAIModelReasoningByName[TModel]95+ : OpenAIModelReasoningByName[TModel] & {96+ offOnlyOptions: 'temperature' | 'top_p' | 'top_logprobs'tombeckenham assignedtombeckenham
github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: maintainer
tombeckenham pushedcf5c3e1
tanstack-source reviewed
@@ -83,7 +84,16 @@8384 ? OpenAIModelInputModalitiesByName[TModel]8485 : readonly ['text', 'image', 'audio']858686-/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */87+/**88+ * The reasoning levels of a model, for `chat({ reasoning })`. `never`: none.89+ * A reasoning model takes the sampling options only with `reasoning: 'off'`.90+ * `openAIModelRejectsSamplingParams` uses the same rule at runtime. A model91+ * that it matches by name only has no sampling options in its type.92+ */8793 type ResolveReasoning<TModel extends string> =8894 TModel extends keyof OpenAIModelReasoningByName89- ? OpenAIModelReasoningByName[TModel]95+ ? TModel extends `${string}-chat-latest`96+ ? OpenAIModelReasoningByName[TModel]97+ : OpenAIModelReasoningByName[TModel] & {98+ offOnlyOptions: OpenAIReasoningSamplingKeys99+ }- @@ -37,4 +38,4 @@37383839 Read the report lines in the output. Each line is a call that the codemod did not change. Move those options by hand with the [key table](#provider-key-reference).3940 If you use Grok, change `max_tokens` to `max_output_tokens` in each Grok `modelOptions`. The codemod writes `max_tokens` for Grok, but the Grok adapter reads `max_output_tokens`.40-Run your type checker.41+If you use an OpenAI reasoning model (`o` models, `gpt-5*`, `gpt-6*`), add `reasoning: 'off'` by hand. The codemod does not add it. Without it, `temperature`, `top_p` and `top_logprobs` are a type error. A model with no `'off'` level cannot take these options, so remove them.@@ -3044,5 +3047,8 @@30443047 [O3_PRO.name]: OpenAIBaseOptions & OpenAIMetadataOptions30453048 [O3_MINI.name]: OpenAIBaseOptions & OpenAIMetadataOptions30463049 [O4_MINI.name]: OpenAIBaseOptions & OpenAIMetadataOptions3047- [O3_DEEP_RESEARCH.name]: OpenAIBaseOptions & OpenAIMetadataOptions3048- [O4_MINI_DEEP_RESEARCH.name]: OpenAIBaseOptions & OpenAIMetadataOptions3050+ // No reasoning data and no `off` level, so the sampling keys are never3051+ // sent. `openAIModelRejectsSamplingParams` matches these by name.3052+ [O3_DEEP_RESEARCH.name]: Omit<3053+ OpenAIBaseOptions,3054+ OpenAIReasoningSamplingKeys@@ -557,3 +574,21 @@557574 threadId?: string558575 }559576577+/**578+ * The `chat()` fields whose types come from the `reasoning` option. The579+ * default `any` takes every `modelOptions` key, for the internal types that580+ * do not infer `reasoning`.581+ */582+type ReasoningChatFields<TAdapter extends AnyTextAdapter, TReasoningOption> = {583+ /**584+ * Model-specific provider options (type comes from adapter). On some585+ * reasoning models, sampling options such as `temperature` need586+ * `reasoning: 'off'`.587+ */588+ modelOptions?: ModelOptionsForReasoning<589+ TAdapter['~types']['providerOptions'],590+ AdapterReasoning<TAdapter>,591+ TReasoningOption592+ >593+ /** {@link TextActivityOptions.reasoning} */594+ reasoning?: TReasoningOption
tombeckenham pushed953f476
tanstack-source reviewed
@@ -83,7 +84,15 @@8384 ? OpenAIModelInputModalitiesByName[TModel]8485 : readonly ['text', 'image', 'audio']858686-/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */87+/**88+ * The reasoning levels of a model, for `chat({ reasoning })`. `never`: none.89+ * A reasoning model takes the sampling options only with `reasoning: 'off'`.90+ * `openAIModelRejectsSamplingParams` uses the same rule at runtime. A model91+ * that it matches by name only has no sampling options in its type.92+ */8793 type ResolveReasoning<TModel extends string> =8894 TModel extends keyof OpenAIModelReasoningByName89- ? OpenAIModelReasoningByName[TModel]95+ ? TModel extends `${string}-chat-latest`96+ ? OpenAIModelReasoningByName[TModel]97+ : OpenAIModelReasoningByName[TModel] & {98+ offOnlyOptions: OpenAIReasoningSamplingKeys@@ -3044,5 +3047,15 @@30443047 [O3_PRO.name]: OpenAIBaseOptions & OpenAIMetadataOptions30453048 [O3_MINI.name]: OpenAIBaseOptions & OpenAIMetadataOptions30463049 [O4_MINI.name]: OpenAIBaseOptions & OpenAIMetadataOptions3047- [O3_DEEP_RESEARCH.name]: OpenAIBaseOptions & OpenAIMetadataOptions3048- [O4_MINI_DEEP_RESEARCH.name]: OpenAIBaseOptions & OpenAIMetadataOptions3050+ // No reasoning data and no `off` level, so the sampling keys are never3051+ // sent. `openAIModelRejectsSamplingParams` matches these by name.3052+ [O3_DEEP_RESEARCH.name]: Omit<3053+ OpenAIBaseOptions,3054+ OpenAIReasoningSamplingKeys3055+ > &3056+ OpenAIMetadataOptions3057+ [O4_MINI_DEEP_RESEARCH.name]: Omit<3058+ OpenAIBaseOptions,3059+ OpenAIReasoningSamplingKeys3060+ > &3061+ OpenAIMetadataOptions
AlemTuzlak pushed47e14ed
tanstack-source reviewed
@@ -83,7 +84,15 @@8384 ? OpenAIModelInputModalitiesByName[TModel]8485 : readonly ['text', 'image', 'audio']858686-/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */87+/**88+ * The reasoning levels of a model, for `chat({ reasoning })`. `never`: none.89+ * A reasoning model takes the sampling options only with `reasoning: 'off'`.90+ * `openAIModelRejectsSamplingParams` uses the same rule at runtime. A model91+ * that it matches by name only has no sampling options in its type.92+ */8793 type ResolveReasoning<TModel extends string> =8894 TModel extends keyof OpenAIModelReasoningByName89- ? OpenAIModelReasoningByName[TModel]95+ ? TModel extends `${string}-chat-latest`96+ ? OpenAIModelReasoningByName[TModel]97+ : OpenAIModelReasoningByName[TModel] & {98+ offOnlyOptions: OpenAIReasoningSamplingKeys@@ -3044,5 +3047,15 @@30443047 [O3_PRO.name]: OpenAIBaseOptions & OpenAIMetadataOptions30453048 [O3_MINI.name]: OpenAIBaseOptions & OpenAIMetadataOptions30463049 [O4_MINI.name]: OpenAIBaseOptions & OpenAIMetadataOptions3047- [O3_DEEP_RESEARCH.name]: OpenAIBaseOptions & OpenAIMetadataOptions3048- [O4_MINI_DEEP_RESEARCH.name]: OpenAIBaseOptions & OpenAIMetadataOptions3050+ // No reasoning data and no `off` level, so the sampling keys are never3051+ // sent. `openAIModelRejectsSamplingParams` matches these by name.3052+ [O3_DEEP_RESEARCH.name]: Omit<3053+ OpenAIBaseOptions,3054+ OpenAIReasoningSamplingKeys3055+ > &3056+ OpenAIMetadataOptions3057+ [O4_MINI_DEEP_RESEARCH.name]: Omit<3058+ OpenAIBaseOptions,3059+ OpenAIReasoningSamplingKeys3060+ > &3061+ OpenAIMetadataOptions@@ -0,0 +88,21 @@88+ const all = readModels(config.file)89+ // Models that OpenRouter does not list cannot be checked here.90+ const models = all.flatMap((meta) => {91+ const params = catalogParams(config.provider, meta.model)92+ return params ? [{ ...meta, params }] : []93+ })94+95+ it('reads every model constant of the file', () => {96+ const content = readFileSync(resolve(ROOT, config.file), 'utf8')97+ const declared = content.match(/^const \w+ = \{\n {2}(?:name|id): '/gm)98+ expect(all.length).toBe(declared?.length)99+ })100+101+ it('finds the models of the catalog', () => {102+ expect(models.length).toBeGreaterThan(10)103+ })104+105+ it('has reasoning data for each model that OpenRouter says reasons', () => {106+ const missing = models107+ .filter((meta) => meta.params.includes('reasoning_effort'))108+ .filter((meta) => !/\n {2}reasoning: /.test(meta.source))- @@ -865,3 +902,6 @@865902 > = [],866903 TContext = unknown,867904 const TMiddleware extends Array<unknown> | undefined = undefined,905+ const TReasoningOption extends906+ | ReasoningOptionFor<AdapterReasoning<TAdapter>>907+ | undefined = undefined,
AlemTuzlak pushedd52e56e
tanstack-source reviewed
@@ -83,8 +84,17 @@8384 ? OpenAIModelInputModalitiesByName[TModel]8485 : readonly ['text', 'image', 'audio']858686-/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */87+/**88+ * The reasoning levels of a model, for `chat({ reasoning })`. `never`: none.89+ * A reasoning model takes the sampling options only with `reasoning: 'off'`.90+ * `openAIModelRejectsSamplingParams` uses the same rule at runtime. A model91+ * that it matches by name only has no sampling options in its type.92+ */8793 type ResolveReasoning<TModel extends string> =8894 TModel extends keyof OpenAIModelReasoningByName89- ? OpenAIModelReasoningByName[TModel]95+ ? TModel extends `${string}-chat-latest`96+ ? OpenAIModelReasoningByName[TModel]97+ : OpenAIModelReasoningByName[TModel] & {98+ offOnlyOptions: OpenAIReasoningSamplingKeys99+ }90100 : never@@ -0,0 +91,21 @@91+ const params = catalogParams(config.provider, meta.model)92+ return params ? [{ ...meta, params }] : []93+ })94+95+ it('reads every model constant of the file', () => {96+ const content = readFileSync(resolve(ROOT, config.file), 'utf8')97+ const declared = content.match(/^const \w+ = \{\n {2}(?:name|id): '/gm)98+ expect(all.length).toBe(declared?.length)99+ })100+101+ it('finds the models of the catalog', () => {102+ expect(models.length).toBeGreaterThan(10)103+ })104+105+ it('has reasoning data for each model that OpenRouter says reasons', () => {106+ const missing = models107+ .filter((meta) => meta.params.includes('reasoning_effort'))108+ .filter((meta) => !/\n {2}reasoning: /.test(meta.source))109+ .map((meta) => meta.model)110+ .filter((model) => !EXCEPTIONS[`${config.provider}:${model}:reasoning`])111+ expect(missing).toEqual([])
tombeckenham pushedac1d33f
tanstack-source reviewed
@@ -83,8 +84,17 @@8384 ? OpenAIModelInputModalitiesByName[TModel]8485 : readonly ['text', 'image', 'audio']858686-/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */87+/**88+ * The reasoning levels of a model, for `chat({ reasoning })`. `never`: none.89+ * A reasoning model takes the sampling options only with `reasoning: 'off'`.90+ * `openAIModelRejectsSamplingParams` uses the same rule at runtime. A model91+ * that it matches by name only has no sampling options in its type.92+ */8793 type ResolveReasoning<TModel extends string> =8894 TModel extends keyof OpenAIModelReasoningByName89- ? OpenAIModelReasoningByName[TModel]95+ ? TModel extends `${string}-chat-latest`96+ ? OpenAIModelReasoningByName[TModel]97+ : OpenAIModelReasoningByName[TModel] & {98+ offOnlyOptions: OpenAIReasoningSamplingKeys99+ }90100 : never@@ -3044,5 +3047,15 @@30443047 [O3_PRO.name]: OpenAIBaseOptions & OpenAIMetadataOptions30453048 [O3_MINI.name]: OpenAIBaseOptions & OpenAIMetadataOptions30463049 [O4_MINI.name]: OpenAIBaseOptions & OpenAIMetadataOptions3047- [O3_DEEP_RESEARCH.name]: OpenAIBaseOptions & OpenAIMetadataOptions3048- [O4_MINI_DEEP_RESEARCH.name]: OpenAIBaseOptions & OpenAIMetadataOptions3050+ // No reasoning data and no `off` level, so the sampling keys are never3051+ // sent. `openAIModelRejectsSamplingParams` matches these by name.3052+ [O3_DEEP_RESEARCH.name]: Omit<3053+ OpenAIBaseOptions,3054+ OpenAIReasoningSamplingKeys3055+ > &3056+ OpenAIMetadataOptions3057+ [O4_MINI_DEEP_RESEARCH.name]: Omit<3058+ OpenAIBaseOptions,3059+ OpenAIReasoningSamplingKeys3060+ > &3061+ OpenAIMetadataOptions@@ -37,4 +38,4 @@37383839 Read the report lines in the output. Each line is a call that the codemod did not change. Move those options by hand with the [key table](#provider-key-reference).3940 If you use Grok, change `max_tokens` to `max_output_tokens` in each Grok `modelOptions`. The codemod writes `max_tokens` for Grok, but the Grok adapter reads `max_output_tokens`.40-Run your type checker.41+If you use an OpenAI reasoning model (`o` models, `gpt-5*`, `gpt-6*`), add `reasoning: 'off'` by hand. The codemod does not add it. Without it, `temperature`, `top_p` and `top_logprobs` are a type error. A model with no `'off'` level cannot take these options, so remove them.github-actions[bot](GitHub profile, opens in a new tab) added labelswaiting-on: author
github-actions[bot](GitHub profile, opens in a new tab) removed labelswaiting-on: maintainer
tombeckenham pushed0b316b8
tombeckenham approved these changes
tanstack-source reviewed
@@ -83,7 +84,15 @@8384 ? OpenAIModelInputModalitiesByName[TModel]8485 : readonly ['text', 'image', 'audio']858686-/** The reasoning levels of a model, for `chat({ reasoning })`. `never`: none. */87+/**88+ * The reasoning levels of a model, for `chat({ reasoning })`. `never`: none.89+ * A reasoning model takes the sampling options only with `reasoning: 'off'`.90+ * `openAIModelRejectsSamplingParams` uses the same rule at runtime. A model91+ * that it matches by name only has no sampling options in its type.92+ */8793 type ResolveReasoning<TModel extends string> =8894 TModel extends keyof OpenAIModelReasoningByName89- ? OpenAIModelReasoningByName[TModel]95+ ? TModel extends `${string}-chat-latest`96+ ? OpenAIModelReasoningByName[TModel]97+ : OpenAIModelReasoningByName[TModel] & {98+ offOnlyOptions: OpenAIReasoningSamplingKeys@@ -37,4 +38,4 @@37383839 Read the report lines in the output. Each line is a call that the codemod did not change. Move those options by hand with the [key table](#provider-key-reference).3940 If you use Grok, change `max_tokens` to `max_output_tokens` in each Grok `modelOptions`. The codemod writes `max_tokens` for Grok, but the Grok adapter reads `max_output_tokens`.40-Run your type checker.41+If you use an OpenAI reasoning model (`o` models, `gpt-5*`, `gpt-6*`), add `reasoning: 'off'` by hand. The codemod does not add it. Without it, `temperature`, `top_p` and `top_logprobs` are a type error. A model with no `'off'` level cannot take these options, so remove them.
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: 0b316b87cc8b89bc77a1c9937c2b1cf5015e30f6
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR