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
1 change: 1 addition & 0 deletions js/plugins/anthropic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"build:watch": "tsup-node --watch",
"test": "tsx --test tests/*_test.ts",
"test:file": "tsx --test",
"test:live": "tsx --test tests/live_test.ts",
"test:coverage": "check-node-version --node '>=22' && tsx --test --experimental-test-coverage --test-coverage-include='src/**/*.ts' ./tests/**/*_test.ts"
}
}
18 changes: 15 additions & 3 deletions js/plugins/anthropic/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ function commonRef(
});
}

/**
* Maps short model names to their full API model IDs.
* Claude 3.x models require versioned names (e.g., claude-3-5-haiku-20241022).
* Claude 4.x models have API aliases that work directly.
*/
const MODEL_VERSION_MAP: Record<string, string> = {
'claude-3-haiku': 'claude-3-haiku-20240307',
'claude-3-5-haiku': 'claude-3-5-haiku-20241022',
};

export const KNOWN_CLAUDE_MODELS: Record<
string,
ModelReference<
Expand Down Expand Up @@ -94,14 +104,16 @@ export const KNOWN_CLAUDE_MODELS: Record<
};

/**
* Gets the un-prefixed model name from a modelReference.
* Gets the API model ID from a model name.
* Maps short names to full versioned names for Claude 3.x models.
* Claude 4.x models pass through unchanged as they have API aliases.
*/
export function extractVersion(
model: ModelReference<ConfigSchemaType> | undefined,
modelName: string
): string {
// Extract from model name (remove 'anthropic/' prefix if present)
return modelName.replace(/^anthropic\//, '');
const cleanName = modelName.replace(/^anthropic\//, '');
return MODEL_VERSION_MAP[cleanName] ?? cleanName;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion js/plugins/anthropic/tests/beta_runner_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ describe('BetaRunner', () => {
true
);

assert.strictEqual(body.model, 'claude-3-5-haiku');
assert.strictEqual(body.model, 'claude-3-5-haiku-20241022');
assert.ok(Array.isArray(body.system));
assert.strictEqual(body.max_tokens, 128);
assert.strictEqual(body.top_k, 4);
Expand Down
83 changes: 83 additions & 0 deletions js/plugins/anthropic/tests/live_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Live integration tests that call the real Anthropic API.
* Only runs when ANTHROPIC_API_KEY environment variable is set.
*
* Run with: ANTHROPIC_API_KEY=your-key pnpm test:live
*/

import * as assert from 'assert';
import { genkit } from 'genkit';
import { describe, it } from 'node:test';
import { anthropic } from '../src/index.js';

const API_KEY = process.env.ANTHROPIC_API_KEY;

describe('Live Anthropic API Tests', { skip: !API_KEY }, () => {
it('should work with short model name claude-3-5-haiku', async () => {
const ai = genkit({
plugins: [anthropic({ apiKey: API_KEY })],
});

const result = await ai.generate({
model: 'anthropic/claude-3-5-haiku',
prompt: 'Say "hello" and nothing else.',
});

assert.ok(result.text.toLowerCase().includes('hello'));
});

it('should work with short model name claude-3-haiku', async () => {
const ai = genkit({
plugins: [anthropic({ apiKey: API_KEY })],
});

const result = await ai.generate({
model: 'anthropic/claude-3-haiku',
prompt: 'Say "hello" and nothing else.',
});

assert.ok(result.text.toLowerCase().includes('hello'));
});

it('should work with full versioned model name', async () => {
const ai = genkit({
plugins: [anthropic({ apiKey: API_KEY })],
});

const result = await ai.generate({
model: 'anthropic/claude-3-5-haiku-20241022',
prompt: 'Say "hello" and nothing else.',
});

assert.ok(result.text.toLowerCase().includes('hello'));
});

it('should work with anthropic.model() helper', async () => {
const ai = genkit({
plugins: [anthropic({ apiKey: API_KEY })],
});

const result = await ai.generate({
model: anthropic.model('claude-3-5-haiku'),
prompt: 'Say "hello" and nothing else.',
});

assert.ok(result.text.toLowerCase().includes('hello'));
});
});
12 changes: 6 additions & 6 deletions js/plugins/anthropic/tests/stable_runner_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ describe('toAnthropicRequestBody', () => {
role: 'user',
},
],
model: 'claude-3-5-haiku',
model: 'claude-3-5-haiku-20241022',
metadata: {
user_id: 'exampleUser123',
},
Expand Down Expand Up @@ -1003,7 +1003,7 @@ describe('toAnthropicRequestBody', () => {
role: 'user',
},
],
model: 'claude-3-haiku',
model: 'claude-3-haiku-20240307',
metadata: {
user_id: 'exampleUser123',
},
Expand Down Expand Up @@ -1220,7 +1220,7 @@ describe('toAnthropicStreamingRequestBody', () => {
);

assert.strictEqual(output.stream, true);
assert.strictEqual(output.model, 'claude-3-5-haiku');
assert.strictEqual(output.model, 'claude-3-5-haiku-20241022');
assert.strictEqual(output.max_tokens, 4096);
});

Expand Down Expand Up @@ -1291,7 +1291,7 @@ describe('claudeRunner', () => {
assert.strictEqual(createStub.mock.calls.length, 1);
assert.deepStrictEqual(createStub.mock.calls[0].arguments, [
{
model: 'claude-3-5-haiku',
model: 'claude-3-5-haiku-20241022',
max_tokens: 4096,
messages: [],
},
Expand Down Expand Up @@ -1342,7 +1342,7 @@ describe('claudeRunner', () => {
assert.strictEqual(streamStub.mock.calls.length, 1);
assert.deepStrictEqual(streamStub.mock.calls[0].arguments, [
{
model: 'claude-3-5-haiku',
model: 'claude-3-5-haiku-20241022',
max_tokens: 4096,
messages: [],
stream: true,
Expand Down Expand Up @@ -2202,7 +2202,7 @@ describe('Runner request bodies and error branches', () => {
true
);

assert.strictEqual(body.model, 'claude-3-5-haiku');
assert.strictEqual(body.model, 'claude-3-5-haiku-20241022');
assert.ok(Array.isArray(body.system));
assert.strictEqual(body.system?.[0].cache_control?.type, 'ephemeral');
assert.strictEqual(body.max_tokens, 256);
Expand Down