createReactAgent no longer works with ChatAnthropic #99

Closed
opened 2026-02-15 17:15:45 -05:00 by yindo · 1 comment
Owner

Originally created by @Pckool on GitHub (Sep 26, 2024).

I'm using createReactAgent, and when I pass an OpenAI model it works fine, but when I pass an anthropic model, the model never get's the messages passed to the agent graph, even though it's there on the top level of the trace. Once it get's into the RunnableLanbda scope, the messages property is lost somehow? The system message is still there, but the other messages passed are not. I'm on the latest version for all packages:

{
  "@langchain/community": "0.3.2",
  "@langchain/core": "0.3.3",
  "@langchain/langgraph": "0.2.8",
  "@langchain/anthropic": "0.3.3",
  "@langchain/openai": "0.3.2",
  "langchain": "0.3.2",
}

Pics of langsmith traces below:
CleanShot 2024-09-26 at 15 39 10@2x
CleanShot 2024-09-26 at 15 39 54@2x

Here's an example of how I'm calling it:

export const claude35Sonnet = new ChatAnthropic({
  modelName: 'claude-3-5-sonnet-20240620',
  temperature: 0.45,
  streaming: true,
  anthropicApiKey: env.ANTHROPIC_API_KEY,
});

const prompt = ChatPromptTemplate.fromMessages(
  [
    SystemMessagePromptTemplate.fromTemplate(`some custom system instructions`),
  ]
);

const agent = createReactAgent({ 
  llm: model, 
  tools: [
    // imagine a list of tools here
  ], 
  messageModifier: prompt 
});

const res = await agent.invoke({
  messages: [
    ['human', 'some random text, please do something for me AI :)']
  ]
})

I tried adding a placeholder for messages as well but it still throws an error, this time saying it's missing the input variable

export const claude35Sonnet = new ChatAnthropic({
  modelName: 'claude-3-5-sonnet-20240620',
  temperature: 0.45,
  streaming: true,
  anthropicApiKey: env.ANTHROPIC_API_KEY,
});

const prompt = ChatPromptTemplate.fromMessages(
  [
    SystemMessagePromptTemplate.fromTemplate(`some custom system instructions`),
    ['placeholder', '{messages}']
  ]
);

const agent = createReactAgent({ 
  llm: claude35Sonnet, 
  tools: [
    // imagine a list of tools here
  ], 
  messageModifier: prompt 
});

const res = await agent.invoke({
  messages: [
    ['human', 'some random text, please do something for me AI :)']
  ]
})

Traces:
image
image

Originally created by @Pckool on GitHub (Sep 26, 2024). I'm using createReactAgent, and when I pass an OpenAI model it works fine, but when I pass an anthropic model, the model never get's the messages passed to the agent graph, even though it's there on the top level of the trace. Once it get's into the `RunnableLanbda` scope, the `messages` property is lost somehow? The system message is still there, but the other messages passed are not. I'm on the latest version for all packages: ```json { "@langchain/community": "0.3.2", "@langchain/core": "0.3.3", "@langchain/langgraph": "0.2.8", "@langchain/anthropic": "0.3.3", "@langchain/openai": "0.3.2", "langchain": "0.3.2", } ``` Pics of langsmith traces below: ![CleanShot 2024-09-26 at 15 39 10@2x](https://github.com/user-attachments/assets/fc8ff843-bb80-4cea-b23a-09914b5bcc55) ![CleanShot 2024-09-26 at 15 39 54@2x](https://github.com/user-attachments/assets/9ed4a389-be3d-4afc-a57d-d928315200fa) Here's an example of how I'm calling it: ```ts export const claude35Sonnet = new ChatAnthropic({ modelName: 'claude-3-5-sonnet-20240620', temperature: 0.45, streaming: true, anthropicApiKey: env.ANTHROPIC_API_KEY, }); const prompt = ChatPromptTemplate.fromMessages( [ SystemMessagePromptTemplate.fromTemplate(`some custom system instructions`), ] ); const agent = createReactAgent({ llm: model, tools: [ // imagine a list of tools here ], messageModifier: prompt }); const res = await agent.invoke({ messages: [ ['human', 'some random text, please do something for me AI :)'] ] }) ``` I tried adding a placeholder for messages as well but it still throws an error, this time saying it's missing the input variable ```ts export const claude35Sonnet = new ChatAnthropic({ modelName: 'claude-3-5-sonnet-20240620', temperature: 0.45, streaming: true, anthropicApiKey: env.ANTHROPIC_API_KEY, }); const prompt = ChatPromptTemplate.fromMessages( [ SystemMessagePromptTemplate.fromTemplate(`some custom system instructions`), ['placeholder', '{messages}'] ] ); const agent = createReactAgent({ llm: claude35Sonnet, tools: [ // imagine a list of tools here ], messageModifier: prompt }); const res = await agent.invoke({ messages: [ ['human', 'some random text, please do something for me AI :)'] ] }) ``` Traces: <img width="1299" alt="image" src="https://github.com/user-attachments/assets/456f97ed-8270-4d34-86f5-426b194d96ad"> <img width="1290" alt="image" src="https://github.com/user-attachments/assets/50aedd22-97e7-471a-9eb8-e2c7a9408dbb">
yindo closed this issue 2026-02-15 17:15:45 -05:00
Author
Owner

@Pckool commented on GitHub (Sep 26, 2024):

Found the issue, I was using ChatMesssage to make the message objects. But that doesn't change the role name for different chat models. Open ai and anthropic have different role's, so this won't work.

ex.

const formattedMessage: ChatMessageFieldWithRole = {
    name: 'a-name-for-the-msg-sender',
    role: 'human',
    content: 'some message content',
};
const messages = [new ChatMessage(formattedMessage)]
// use it in an anthropic REACT agent model call and it will have the issue

This code will have the issue described here

@Pckool commented on GitHub (Sep 26, 2024): Found the issue, I was using `ChatMesssage` to make the message objects. But that doesn't change the role name for different chat models. Open ai and anthropic have different role's, so this won't work. ex. ```ts const formattedMessage: ChatMessageFieldWithRole = { name: 'a-name-for-the-msg-sender', role: 'human', content: 'some message content', }; const messages = [new ChatMessage(formattedMessage)] // use it in an anthropic REACT agent model call and it will have the issue ``` This code will have the issue described here
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraphjs#99