langchain-ai / langchain-google

MIT License
117 stars 149 forks source link

AI Message Compilation Error with Tool Call and List Content #563

Open AustinJiangH opened 1 month ago

AustinJiangH commented 1 month ago

There is a chance that the model returns an AI message with the content being a list, AIMessage(content=[], tool_calls=[...]).

When involving this message in the conversation, the tool calls gets trimmed out. It runs into the if block on line 83 because the content type is list and never gets into the elif block for the tool call on line 141.

https://github.com/langchain-ai/langchain-google/blob/af44de36cb82277bb5e535e6c2ba968c5b52a18d/libs/vertexai/langchain_google_vertexai/_anthropic_utils.py#L83-L141

This will eventually get into a bad request error.

Error code: 400 - {'type': 'error', 'error': {'type': 'invalid_request_error', 'message': 'messages.2: `tool_result` block(s) provided when previous message does not contain any `tool_use` blocks'}}
lkuligin commented 1 month ago

thanks for spotting this!

would you be open to send a PR with a fix (and ideally also add a unit test for this case)?