-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Anthropic API error when using prebuilt create_react_agent after model ends turn with empty content message #3168
Comments
When you send any empty string in messages to Anthropic, Anthropic will return a 400 error ( For example, both of the following cases will throw this error. from dotenv import load_dotenv
from langchain_anthropic import ChatAnthropic
load_dotenv()
print(ChatAnthropic(model="claude-3-5-sonnet-latest").invoke("")) # 400 error
messages = [
("human", "Hi, I'm Alice."),
("ai", ""),
("human", "How are you doing?"),
]
print(ChatAnthropic(model="claude-3-5-sonnet-latest").invoke(messages)) # 400 error I think this issue is caused by the last sentence of your prompt, which seems to be a design flaw in the prompt. |
@gbaian10 yes — i should have called this out in the original message. I think this issue is still a bug for the following reasons:
|
@maxmamis perhaps it would be sufficient to set |
@vbarda interesting, that might be what I'm looking for. thanks for the tip! |
@maxmamis great! could you please confirm that this resolves your issue and we can close this one then |
@vbarda Getting the same issue after setting the return_direct parameter. |
@owais3901 did you use the same example as above with |
Could folks please confirm if |
Checked other resources
Example Code
Error Message and Stack Trace (if applicable)
Description
When using the prebuilt
create_react_agent
with Anthropic models, in some cases the agent will end a turn with a message with empty content. For example, my demo code produces the following output after the first user message:Note the empty content in the final AIMessage. On the next turn, this message is included in the input to the agent, and causes Anthropic to return an error:
I can filter these empty messages out manually, but I would expect this behavior to be included in the prebuilt workflow. Additionally, filtering out empty messages in this example will cause errors when using OpenAI models, so in order to be model-agnostic, I need to carefully special-case my handling.
Expected Result: No error, agent continues as normal
Observed Result: Error is thrown on any subsequent message sent to this thread
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
The text was updated successfully, but these errors were encountered: