You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using anthropic provider with Haiku 3.5, and calling addToolResult from the client, I'm getting the following error:
APICallError [AI_APICallError]: Your API request included an `assistant` message in the final position, which would pre-fill the `assistant` response. When using tools, pre-filling the `assistant` response is not supported.
Code example
'use client';import{ToolInvocation}from'ai';import{Message,useChat}from'ai/react';exportdefaultfunctionChat(){const{ messages, input, handleInputChange, handleSubmit, addToolResult }=useChat({maxSteps: 5,// run client-side tools that are automatically executed:asynconToolCall({ toolCall }){if(toolCall.toolName==='getLocation'){constcities=['New York','Los Angeles','Chicago','San Francisco',];returncities[Math.floor(Math.random()*cities.length)];}},});return(<>{messages?.map((m: Message)=>(<divkey={m.id}><strong>{m.role}:</strong>{m.content}{m.toolInvocations?.map((toolInvocation: ToolInvocation)=>{consttoolCallId=toolInvocation.toolCallId;constaddResult=(result: string)=>addToolResult({ toolCallId, result });// render confirmation tool (client-side tool with user interaction)if(toolInvocation.toolName==='askForConfirmation'){return(<divkey={toolCallId}>{toolInvocation.args.message}<div>{'result'intoolInvocation ? (<b>{toolInvocation.result}</b>
) : (<><buttononClick={()=>addResult('Yes')}>Yes</button><buttononClick={()=>addResult('No')}>No</button></>)}</div></div>);}// other tools:return'result'intoolInvocation ? (<divkey={toolCallId}>Toolcall{`${toolInvocation.toolName}: `}{toolInvocation.result}</div>) : (<divkey={toolCallId}>Calling{toolInvocation.toolName}...</div>
);})}<br/></div>))}<formonSubmit={handleSubmit}><inputvalue={input}onChange={handleInputChange}/></form></>);}
AI provider
@ai-sdk/anthropic v1.0.6
Additional context
ai v4.0.26
The text was updated successfully, but these errors were encountered:
Sure, this is how the route looks. I'm using Hono btw.
routes.openapi("...",async(c)=>{const{ messages, chatId }=c.req.valid("json");constanthropic=createAnthropic({apiKey: c.env.ANTHROPIC_API_KEY,baseURL:
"https://gateway.ai.cloudflare.com/v1/[redacted]/[redacted]/anthropic",});constmodel=anthropic("claude-3-5-haiku-latest");constdataStream=createDataStream({execute: async(dataStreamWriter)=>{constresult=streamText({
model,
messages,tools: {askUserToMakeAChoice: tool({description:
"Ask the user to choose an option from a list of options",parameters: z.object({options: z.string().array().describe("The options to present to the user"),}),}),},maxSteps: 5,maxRetries: 10,experimental_transform: smoothStream(),system: chatPrompt(),onFinish: ()=>{dataStreamWriter.writeData("done");},});result.mergeIntoDataStream(dataStreamWriter);},});c.header("X-Vercel-AI-Data-Stream","v1");c.header("Content-Type","text/plain; charset=utf-8");c.header("Content-Encoding","Identity");returnstream(c,(stream)=>stream.pipe(dataStream.pipeThrough(newTextEncoderStream())));});
Description
When using anthropic provider with Haiku 3.5, and calling
addToolResult
from the client, I'm getting the following error:Code example
AI provider
@ai-sdk/anthropic v1.0.6
Additional context
ai
v4.0.26The text was updated successfully, but these errors were encountered: