Repository navigation
fix(promptfoo): replace retired Claude Sonnet 4 default with Claude Sonnet 5.5 #181
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,18 @@ | |
| * Allows the agent to work with OpenAI or Anthropic. | ||
| */ | ||
|
|
||
| type AnthropicContentBlock = | ||
| | { type: 'text'; text: string } | ||
| | { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> } | ||
| | { type: 'thinking'; thinking: string; signature: string } | ||
| | { type: 'redacted_thinking'; data: string }; | ||
|
|
||
| export interface Message { | ||
| role: 'system' | 'user' | 'assistant' | 'tool'; | ||
| content: string; | ||
| toolCallId?: string; | ||
| toolCalls?: ToolCall[]; | ||
| anthropicContent?: AnthropicContentBlock[]; | ||
| } | ||
|
|
||
| export interface ToolCall { | ||
|
|
@@ -28,6 +35,7 @@ export interface ChatOptions { | |
| export interface ChatResponse { | ||
| content: string | null; | ||
| toolCalls: ToolCall[]; | ||
| anthropicContent?: AnthropicContentBlock[]; | ||
| finishReason: 'stop' | 'tool_calls' | 'length' | 'error'; | ||
| } | ||
|
|
||
|
|
@@ -154,7 +162,7 @@ export class AnthropicProvider implements LLMProvider { | |
|
|
||
| constructor(options: { apiKey?: string; model?: string; baseUrl?: string }) { | ||
| this.apiKey = options.apiKey || process.env.ANTHROPIC_API_KEY || ''; | ||
| this.model = options.model || 'claude-sonnet-4-20250514'; | ||
| this.model = options.model || 'claude-sonnet-5-5'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When Sonnet 5.5 uses its default adaptive thinking before a tool call, the existing response parser discards its Useful? React with 👍 / 👎. |
||
| this.baseUrl = options.baseUrl || 'https://api.anthropic.com/v1'; | ||
|
|
||
| if (!this.apiKey) { | ||
|
|
@@ -179,7 +187,10 @@ export class AnthropicProvider implements LLMProvider { | |
| messages: nonSystemMessages.map((m) => this.toAnthropicMessage(m)), | ||
| tools: options.tools, | ||
| max_tokens: options.maxTokens || 4096, | ||
| temperature: options.temperature ?? 0.7, | ||
| // Claude Sonnet 5.5 rejects sampling parameters even when explicitly set. | ||
| ...(this.model !== 'claude-sonnet-5-5' && options.temperature !== undefined | ||
| ? { temperature: options.temperature } | ||
| : {}), | ||
| }), | ||
| }); | ||
|
|
||
|
|
@@ -189,10 +200,7 @@ export class AnthropicProvider implements LLMProvider { | |
| } | ||
|
|
||
| const data = (await response.json()) as { | ||
| content: Array< | ||
| | { type: 'text'; text: string } | ||
| | { type: 'tool_use'; id: string; name: string; input: Record<string, unknown> } | ||
| >; | ||
| content: AnthropicContentBlock[]; | ||
| stop_reason: string; | ||
| }; | ||
|
|
||
|
|
@@ -214,6 +222,7 @@ export class AnthropicProvider implements LLMProvider { | |
| return { | ||
| content: textContent || null, | ||
| toolCalls, | ||
| anthropicContent: data.content, | ||
| finishReason: this.mapFinishReason(data.stop_reason), | ||
| }; | ||
| } | ||
|
|
@@ -231,6 +240,10 @@ export class AnthropicProvider implements LLMProvider { | |
| ], | ||
| }; | ||
| } | ||
| if (m.role === 'assistant' && m.anthropicContent?.length) { | ||
| // Anthropic requires the original signed blocks in their original order. | ||
| return { role: 'assistant', content: m.anthropicContent }; | ||
| } | ||
| if (m.role === 'assistant' && m.toolCalls?.length) { | ||
| return { | ||
| role: 'assistant', | ||
|
|
@@ -269,7 +282,7 @@ export function createProvider(provider: string, options?: { reasoningEffort?: s | |
| case 'openai': | ||
| return new OpenAIProvider({ model: model || 'gpt-5', reasoningEffort: options?.reasoningEffort }); | ||
| case 'anthropic': | ||
| return new AnthropicProvider({ model: model || 'claude-sonnet-4-20250514' }); | ||
| return new AnthropicProvider({ model: model || 'claude-sonnet-5-5' }); | ||
| default: | ||
| throw new Error(`Unknown provider: ${type}. Supported: openai, anthropic`); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { afterEach, expect, test, vi } from 'vitest'; | ||
| import { runDiscoveryAgent } from '../src/agent/loop.js'; | ||
| import { AnthropicProvider, createProvider } from '../src/agent/providers.js'; | ||
|
|
||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| vi.unstubAllEnvs(); | ||
| }); | ||
|
|
||
| async function sentBody(chat: () => Promise<unknown>, fetchMock: ReturnType<typeof vi.fn>) { | ||
| await chat(); | ||
| return JSON.parse(fetchMock.mock.calls[0][1].body); | ||
| } | ||
|
|
||
| function stubAnthropic() { | ||
| const fetchMock = vi.fn(async () => | ||
| new Response(JSON.stringify({ content: [{ type: 'text', text: 'ok' }], stop_reason: 'end_turn' })), | ||
| ); | ||
| vi.stubGlobal('fetch', fetchMock); | ||
| vi.stubEnv('ANTHROPIC_API_KEY', 'fixture-key'); | ||
| return fetchMock; | ||
| } | ||
|
|
||
| test.each([ | ||
| ['createProvider', () => createProvider('anthropic')], | ||
| ['AnthropicProvider', () => new AnthropicProvider({})], | ||
| ])('%s defaults to a supported model without sampling parameters', async (_name, build) => { | ||
| const fetchMock = stubAnthropic(); | ||
| const body = await sentBody(() => build().chat({ messages: [], tools: [] }), fetchMock); | ||
| expect(body.model).toBe('claude-sonnet-5-5'); | ||
| expect(body).not.toHaveProperty('temperature'); | ||
| }); | ||
|
|
||
| test('forwards an explicit temperature to Anthropic', async () => { | ||
| const fetchMock = stubAnthropic(); | ||
| const body = await sentBody( | ||
| () => createProvider('anthropic:claude-sonnet-4-6').chat({ messages: [], tools: [], temperature: 0 }), | ||
| fetchMock, | ||
| ); | ||
| expect(body.temperature).toBe(0); | ||
| }); | ||
|
|
||
| test('omits explicit temperature on Sonnet 5.5 and keeps the requested output limit', async () => { | ||
| const fetchMock = stubAnthropic(); | ||
| const body = await sentBody( | ||
| () => createProvider('anthropic:claude-sonnet-5-5').chat({ | ||
| messages: [], | ||
| tools: [], | ||
| temperature: 0, | ||
| maxTokens: 8192, | ||
| }), | ||
| fetchMock, | ||
| ); | ||
| expect(body).not.toHaveProperty('temperature'); | ||
| expect(body.max_tokens).toBe(8192); | ||
| }); | ||
|
|
||
| test('replays signed thinking with Anthropic tool results on the next discovery turn', async () => { | ||
| const content = [ | ||
| { type: 'thinking', thinking: '', signature: 'fixture-signature' }, | ||
| { type: 'text', text: 'I will try a tool.' }, | ||
| { type: 'tool_use', id: 'toolu_first', name: 'unknown_fixture_tool', input: {} }, | ||
| ]; | ||
| const fetchMock = stubAnthropic(); | ||
| fetchMock | ||
| .mockResolvedValueOnce(new Response(JSON.stringify({ content, stop_reason: 'tool_use' }))) | ||
| .mockResolvedValueOnce(new Response(JSON.stringify({ | ||
| content: [{ | ||
| type: 'tool_use', | ||
| id: 'toolu_done', | ||
| name: 'done', | ||
| input: { summary: 'Complete', verified: true }, | ||
| }], | ||
| stop_reason: 'tool_use', | ||
| }))); | ||
|
|
||
| const result = await runDiscoveryAgent({ | ||
| context: 'Fixture target', | ||
| provider: createProvider('anthropic'), | ||
| maxTurns: 2, | ||
| }); | ||
|
|
||
| expect(result.success).toBe(true); | ||
| expect(fetchMock).toHaveBeenCalledTimes(2); | ||
| const body = JSON.parse(fetchMock.mock.calls[1][1].body); | ||
| expect(body.messages.slice(-2)).toEqual([ | ||
| { role: 'assistant', content }, | ||
| { | ||
| role: 'user', | ||
| content: [{ | ||
| type: 'tool_result', | ||
| tool_use_id: 'toolu_first', | ||
| content: JSON.stringify({ error: 'Unknown tool: unknown_fixture_tool' }), | ||
| }], | ||
| }, | ||
| ]); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If adaptive thinking exhausts the provider's 4096-token default before producing text or tool calls, Anthropic returns
max_tokens,response.contentis null, andtoolCallsis empty. This branch then stores the thinking-only assistant message without adding a subsequent user or tool-result turn. The next request consequently ends with that assistant message, which Anthropic rejects because assistant prefilling is unsupported with thinking enabled, aborting discovery. HandlefinishReason === 'length'by retrying with a larger token budget, or explicitly disable thinking, rather than replaying the truncated thinking-only response.Useful? React with 👍 / 👎.