Skip to content

SDK returns empty text blocks, but does not accept them #461

@eyurtsev

Description

@eyurtsev

Using anthropic sdk via langchain_anthropic integration

from langchain_core.messages import AIMessage, HumanMessage, ToolMessage, SystemMessage
from langchain_anthropic import ChatAnthropic
from langchain_core.tools import tool

@tool
def type_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)

The SDK can return empty text blocks from models:

ToolsBetaMessage(id='msg_01D6BbZbwbPYRVMPVu5SZRTp', content=[TextBlock(text='', type='text'), ToolUseBlock(id='toolu_01YHFB1pLY3xNa1edzoV4HtL', input={'letter': 'd'}, name='type_letter', type='tool_use'), ToolUseBlock(id='toolu_011ewRdDypyE8yHMniYT2gzj', input={'letter': 'o'}, name='type_letter', type='tool_use'), ToolUseBlock(id='toolu_01YKV1QaDPePiAFQVwmrSmjk', input={'letter': 'g'}, name='type_letter', type='tool_use')], model='claude-3-haiku-20240307', role='assistant', stop_reason='tool_use', stop_sequence=None, type='message', usage=Usage(input_tokens=395, output_tokens=125))

However, the SDK does not accept empty text block if re-interacting with the model (e.g., if using an agent).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions