https://promptfoo.dev/ logo
Anthropic/claude tool response and javascript asse...
# questions
t
I'm trying to write a javascript assert for an anthropic tool response; but I don't seem to get the output var, tried many things, is there someone with a working example?
w
Can you share your config?
t
the below javascript assert only works for openai, not for anthropic; the output is tool output and correct in both cases, it just seems that the assert doesn't get the anthropic output at all providers: - id: anthropic:claude-sonnet-4-20250514 config: tools: file://./extract-anthropic.json tool_choice: type: 'tool' name: 'extract_document_data' - id: openai:gpt-4o config: tools: file://./extract.json tool_choice: 'required' tests: - vars: ocr_text: file://./data*.txt assert: - type: javascript value: JSON.parse(output[0].function.arguments).issue_date.value === "2025-08-25"
ERROR: Custom function threw error: Cannot read properties of undefined (reading 'arguments') Stack Trace: TypeError: Cannot read properties of undefined (reading 'arguments') at eval (eval at handleJavascript (/Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/dist/src/assertions/javascript.js:49:36), :3:38) at handleJavascript (/Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/dist/src/assertions/javascript.js:50:43) at runAssertion (/Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/dist/src/assertions/index.js:313:30) at /Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/dist/src/assertions/index.js:360:30 at /Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/node_modules/async/dist/async.js:151:38 at replenish (/Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/node_modules/async/dist/async.js:447:21) at iterateeCallback (/Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/node_modules/async/dist/async.js:431:21) at /Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/node_modules/async/dist/async.js:328:20 at invokeCallback (/Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/node_modules/async/dist/async.js:182:13) at /Users/tomvleminckx/.npm/_npx/81bbc6515d992ace/node_modules/promptfoo/node_modules/async/dist/async.js:174:13 JSON.parse(output[0].function.arguments).issue_date.value === "2025-08-25" {"type":"tool_use","id":"toolu_01LjkyRza6ZPLHxe5GJyCKgW","name":"extract_document_data","input":{"issue_date":{"value":"2025-08-25","coordinates":{"x":339,"y":314,"width":142,"height":17,"page_number":1}},"ca_type":{"value":"Takeover","coordinates":{"x":340,"y":548,"width":84,"height":14,"page_number":1}}}} Show less Tokens: 11,680 (11,501+179) Latency: 4,513 ms Tokens/Sec: 40 Cost: $0.037
a
try type: javascript value: | const toolData = output[0].function?.arguments || output[0].input; return JSON.parse(typeof toolData === 'string' ? toolData : JSON.stringify(toolData)) .issue_date.value === "2025-08-25";
t
thanks for the reply; it seems that output is undefined in the anthorpic case; so the suggested solution doesn't work