diff --git a/.changeset/add-anthropic-models-export.md b/.changeset/add-anthropic-models-export.md new file mode 100644 index 00000000..f9df4019 --- /dev/null +++ b/.changeset/add-anthropic-models-export.md @@ -0,0 +1,64 @@ +--- +'@tanstack/ai-anthropic': major +--- + +## Add missing AnthropicModels type export + +### WHAT + +Added new `AnthropicModels` **type** export to the public API of `@tanstack/ai-anthropic`. This is a union type that represents all supported Anthropic model identifiers, derived from the internal `ANTHROPIC_MODELS` const tuple. + +```typescript +export type AnthropicModels = (typeof ANTHROPIC_MODELS)[number] +// Equivalent to: 'claude-opus-4-5' | 'claude-sonnet-4-5' | 'claude-haiku-4-5' | ... (and more) +``` + +**Note:** The `ANTHROPIC_MODELS` const tuple itself remains internal and is not exported. Only the derived type is part of the public API. + +### WHY + +Consumers previously had no easy way to get the type-safe union of model names for use in function signatures and variable declarations. + +### HOW - Consumers Should Update + +Now you can import and use `AnthropicModels` for proper type safety when creating adapter instances: + +```typescript +import { createAnthropicChat, AnthropicModels } from '@tanstack/ai-anthropic' + +const adapter = (model: AnthropicModels) => + createAnthropicChat(model, process.env.ANTHROPIC_API_KEY!, { + // ... your config options + }) + +const stream = chat({ + adapter: adapter('claude-sonnet-4-5'), // Type-checked model selection! + messages: [{ role: 'user', content: 'Hello!' }], +}) +``` + +The type ensures only valid Anthropic model identifiers can be passed, preventing runtime errors. + +### Breaking Change + +`createAnthropicChat` now requires the model as the first parameter. If you were calling it without a model parameter, you must update your code: + +**Before:** + +```typescript +const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, { + // ... config +}) +``` + +**After:** + +```typescript +const adapter = createAnthropicChat( + 'claude-sonnet-4-5', + process.env.ANTHROPIC_API_KEY!, + { + // ... config + }, +) +``` diff --git a/docs/adapters/anthropic.md b/docs/adapters/anthropic.md index 15fd179f..b0bfbd79 100644 --- a/docs/adapters/anthropic.md +++ b/docs/adapters/anthropic.md @@ -28,11 +28,12 @@ const stream = chat({ ```typescript import { chat } from "@tanstack/ai"; -import { createAnthropicChat } from "@tanstack/ai-anthropic"; +import { createAnthropicChat, AnthropicModels } from "@tanstack/ai-anthropic"; -const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, { - // ... your config options -}); +const adapter = (model: AnthropicModels) => + createAnthropicChat(model, process.env.ANTHROPIC_API_KEY!, { + // ... your config options + }); const stream = chat({ adapter: adapter("claude-sonnet-4-5"), @@ -51,7 +52,7 @@ const config: Omit = { const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, config); ``` - + ## Example: Chat Completion diff --git a/packages/typescript/ai-anthropic/src/index.ts b/packages/typescript/ai-anthropic/src/index.ts index 4bca2e4b..fdf8b1b4 100644 --- a/packages/typescript/ai-anthropic/src/index.ts +++ b/packages/typescript/ai-anthropic/src/index.ts @@ -27,6 +27,7 @@ export { export type { AnthropicChatModelProviderOptionsByName, AnthropicModelInputModalitiesByName, + AnthropicModels, } from './model-meta' export type { AnthropicTextMetadata, diff --git a/packages/typescript/ai-anthropic/src/model-meta.ts b/packages/typescript/ai-anthropic/src/model-meta.ts index fa48e503..124b98eb 100644 --- a/packages/typescript/ai-anthropic/src/model-meta.ts +++ b/packages/typescript/ai-anthropic/src/model-meta.ts @@ -372,6 +372,8 @@ export const ANTHROPIC_MODELS = [ CLAUDE_HAIKU_3.id, ] as const +export type AnthropicModels = (typeof ANTHROPIC_MODELS)[number] + // const ANTHROPIC_IMAGE_MODELS = [] as const // const ANTHROPIC_EMBEDDING_MODELS = [] as const // const ANTHROPIC_AUDIO_MODELS = [] as const