Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
64 changes: 64 additions & 0 deletions .changeset/add-anthropic-models-export.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
'@tanstack/ai-anthropic': major
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not a major change, this hsould be set to patch

---

## 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the breaking change section as it's not a 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
},
)
```
11 changes: 6 additions & 5 deletions docs/adapters/anthropic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revert this change please

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
Thanks for your review.

Correct me if I am wrong, but when I was trying to use createAnthropicChat it gives me errors as of the previous docs example. When I see the function definition here I can see that it requires the model in the first parameter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that is the case then shouldn't the docs be updated?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is true but it should be:

const adapter = createAnthropicChat("model", process.env.ANTHROPIC_API_KEY!, {...})

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,
About the example, we can either put the example like this:

import { chat } from '@tanstack/ai'
import { createAnthropicChat } from '@tanstack/ai-anthropic'

const adapter = createAnthropicChat(
  'claude-sonnet-4-5',
  process.env.ANTHROPIC_API_KEY!,
  {
    // ... your config options
  },
)

const stream = chat({
  adapter: adapter,
  messages: [{ role: 'user', content: 'Hello!' }],
})

here the adapter is connected with a single model and we pass it to the chat, but in the other examples in the docs I can see we can change the model names in the chat so its more flexible to try out different model.

So if we put the example like this:

const adapter = (model: AnthropicModels) =>
  createAnthropicChat(model, process.env.ANTHROPIC_API_KEY!, {
    // ... your config options
  })

const stream = chat({
  adapter: adapter('claude-3-5-haiku'),
  messages: [{ role: 'user', content: 'Hello!' }],
})

We can easily swap out different models in the chat like this:

image

Let me know what do you think and if I am thinking in a wrong way 🙏

createAnthropicChat(model, process.env.ANTHROPIC_API_KEY!, {
// ... your config options
});

const stream = chat({
adapter: adapter("claude-sonnet-4-5"),
Expand All @@ -51,7 +52,7 @@ const config: Omit<AnthropicChatConfig, 'apiKey'> = {

const adapter = createAnthropicChat(process.env.ANTHROPIC_API_KEY!, config);
```


## Example: Chat Completion

Expand Down
1 change: 1 addition & 0 deletions packages/typescript/ai-anthropic/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
export type {
AnthropicChatModelProviderOptionsByName,
AnthropicModelInputModalitiesByName,
AnthropicModels,
} from './model-meta'
export type {
AnthropicTextMetadata,
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript/ai-anthropic/src/model-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down