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
{{ message }}
Repository navigation
SDK returns empty text blocks, but does not accept them #461
Using anthropic sdk via langchain_anthropic integration
fromlangchain_core.messagesimportAIMessage, HumanMessage, ToolMessage, SystemMessagefromlangchain_anthropicimportChatAnthropicfromlangchain_core.toolsimporttool@tooldeftype_letter(letter: str) ->str:
"""Type the given letter."""return"OK"model=ChatAnthropic(model="claude-3-haiku-20240307", temperature=0).bind_tools([type_letter])
messages= [
SystemMessage(content="Repeat the given string using the provided tools. Do not write anything else or provide any explanations. For example, if the string is 'abc', you must print the letters 'a', 'b', and 'c' one at a time and in that order. "),
HumanMessage(content="dog")
]
model.invoke(messages)
Here's an example of re-interaction with the model:
fromlangchain_core.messagesimportAIMessage, HumanMessage, ToolMessage, SystemMessagefromlangchain_anthropicimportChatAnthropicfromlangchain_core.toolsimporttool@tooldeftype_letter(letter: str) ->str:
"""Type the given letter."""return"OK"model=ChatAnthropic(model="claude-3-opus-20240229", temperature=0).bind_tools([type_letter])
messages= [
SystemMessage(content="Repeat the given string using the provided tools. Do not write anything else or provide any explanations. For example, if the string is 'abc', you must print the letters 'a', 'b', and 'c' one at a time and in that order. "),
HumanMessage(content="dog"),
AIMessage(
content=[
{"text": "", "type": "text"},
{
"id": "toolu_01V6d6W32QGGSmQm4BT98EKk",
"input": {
"letter": "d"
},
"name": "type_letter",
"type": "tool_use"
},
],
tool_calls=[
{
"name": "type_letter",
"args": {
"letter": "d"
},
"id": "toolu_01V6d6W32QGGSmQm4BT98EKk"
},
],
),
ToolMessage(content="OK", tool_call_id="toolu_01V6d6W32QGGSmQm4BT98EKk"),
]
model.invoke(messages)
This results in a 4xx error:
BadRequestError: Errorcode: 400- {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: text content blocks must be non-empty'}}
But this should be accepted since it's output by the sdk / model itself
Unless I am missing something, I still have the same issue with the following code:
importanthropicclient=anthropic.Client()
messages= [
{
"content": [{"text": "How many appointments do I have today?", "type": "text"}],
"role": "user",
},
{
"content": [
{
"text": '<thinking>\nTo find out how many appointments the user has today, I should use the get_day_calendar_events tool. This tool requires a "day" parameter in the format YYYY-MM-DD.\n\nEmma did not specify a date in her request, so I first need to determine what the current date is. I can use the get_current_day tool to retrieve that information.\n\nOnce I have the current date, I can pass that into the get_day_calendar_events tool to retrieve Emma\'s appointments for today.\n</thinking>',
"type": "text",
},
{
"id": "toolu_01UsBRMJWT8xAssuztCGEZNu",
"input": {},
"name": "get_current_day",
"type": "tool_use",
},
],
"role": "assistant",
},
{
"content": [
{
"tool_use_id": "toolu_01UsBRMJWT8xAssuztCGEZNu",
"type": "tool_result",
"content": [{"text": "2024-05-15", "type": "text"}],
"is_error": False,
}
],
"role": "user",
},
{
"content": [
{"text": "", "type": "text"},
{
"id": "toolu_01YRKLXccBZjeQfYU2M7YLY8",
"input": {"day": "2024-05-15"},
"name": "get_day_calendar_events",
"type": "tool_use",
},
],
"role": "assistant",
},
{
"content": [
{
"tool_use_id": "toolu_01YRKLXccBZjeQfYU2M7YLY8",
"type": "tool_result",
"content": [{"text": "<TOOL OUTPUT HERE>", "type": "text"}],
"is_error": False,
}
],
"role": "user",
},
]
client.messages.create(
model="claude-3-opus-20240229",
max_tokens=1000,
messages=messages,
)
This gives me the following error:
anthropic.BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: text content blocks must be non-empty'}}
Activity
eyurtsev commentedon Apr 17, 2024
Here's an example of re-interaction with the model:
This results in a 4xx error:
But this should be accepted since it's output by the sdk / model itself
hinthornw commentedon Apr 17, 2024
Related code snippet:
aaron-lerner commentedon Apr 17, 2024
Thanks for reporting. Working on fixing this asap!
aaron-lerner commentedon Apr 17, 2024
This is now fixed.
dedeswim commentedon May 31, 2024
Unless I am missing something, I still have the same issue with the following code:
This gives me the following error:
aaron-lerner commentedon May 31, 2024
Hi @dedeswim, was this part of your request given as output by the API or did you put it together yourself?
The API returning 400 for
{"text": "", "type": "text"}is correct, but the API returning this as content is not.dedeswim commentedon May 31, 2024
Hi @aaron-lerner, you are right, it's my fault! Sorry, I somewhat completely missed that line.
Thanks a lot for the quick reply!
mmichelli commentedon Feb 12, 2025
BadRequestError: Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages: text content blocks must be non-empty'}}
I'm getting this issue from AnthropicVertex.
fix(llm/anthropic): add workaround to Anthropic API glitch
fix(llm/anthropic): add workaround to Anthropic API glitch
fix(llm/anthropic): add workaround to Anthropic API glitch
fix(llm/anthropic): add workaround to Anthropic API glitch
POC: native LLM support (#9628)
POC: native LLM support (#9628)
POC: native LLM support (#9628)
POC: native LLM support (dagger#9628)
POC: native LLM support (#9628)