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

@@ -832,6 +832,18 @@ def test_convert_to_openai_messages_anthropic() -> None:
]
assert result == expected
# Test thinking blocks (pass through)
thinking_block = {
"signature": "abc123",
"thinking": "Thinking text.",
"type": "thinking",
}
text_block = {"text": "Response text.", "type": "text"}
messages = [AIMessage([thinking_block, text_block])]
result = convert_to_openai_messages(messages)
expected = [{"role": "assistant", "content": [thinking_block, text_block]}]
assert result == expected
def test_convert_to_openai_messages_bedrock_converse_image() -> None:
image_data = create_image_data()