core, openai, standard-tests: improve OpenAI compatibility with Anthropic content blocks (#30128)

- Support thinking blocks in core's `convert_to_openai_messages` (pass
through instead of error)
- Ignore thinking blocks in ChatOpenAI (instead of error)
- Support Anthropic-style image blocks in ChatOpenAI

---

Standard integration tests include a `supports_anthropic_inputs`
property which is currently enabled only for tests on `ChatAnthropic`.
This test enforces compatibility with message histories of the form:
```
- system message
- human message
- AI message with tool calls specified only through `tool_use` content blocks
- human message containing `tool_result` and an additional `text` block
```
It additionally checks support for Anthropic-style image inputs if
`supports_image_inputs` is enabled.

Here we change this test, such that if you enable
`supports_anthropic_inputs`:
- You support AI messages with text and `tool_use` content blocks
- You support Anthropic-style image inputs (if `supports_image_inputs`
is enabled)
- You support thinking content blocks.

That is, we add a test case for thinking content blocks, but we also
remove the requirement of handling tool results within HumanMessages
(motivated by existing agent abstractions, which should all return
ToolMessage). We move that requirement to a ChatAnthropic-specific test.
This commit is contained in:
ccurme
2025-03-06 09:53:14 -05:00
committed by GitHub
parent b3dc66f7a3
commit 52b0570bec
6 changed files with 146 additions and 14 deletions

View File

@@ -186,15 +186,38 @@ def _convert_dict_to_message(_dict: Mapping[str, Any]) -> BaseMessage:
def _format_message_content(content: Any) -> Any:
"""Format message content."""
if content and isinstance(content, list):
# Remove unexpected block types
formatted_content = []
for block in content:
# Remove unexpected block types
if (
isinstance(block, dict)
and "type" in block
and block["type"] == "tool_use"
and block["type"] in ("tool_use", "thinking")
):
continue
# Anthropic image blocks
elif (
isinstance(block, dict)
and block.get("type") == "image"
and (source := block.get("source"))
and isinstance(source, dict)
):
if source.get("type") == "base64" and (
(media_type := source.get("media_type"))
and (data := source.get("data"))
):
formatted_content.append(
{
"type": "image_url",
"image_url": {"url": f"data:{media_type};base64,{data}"},
}
)
elif source.get("type") == "url" and (url := source.get("url")):
formatted_content.append(
{"type": "image_url", "image_url": {"url": url}}
)
else:
continue
else:
formatted_content.append(block)
else: