mirror of
https://github.com/hwchase17/langchain.git
synced 2026-03-17 18:45:44 +00:00
fix(anthropic): filter out common OpenAI Responses block types (#35417)
This commit is contained in:
@@ -471,6 +471,12 @@ def _format_messages(
|
||||
if "type" not in block:
|
||||
msg = "Dict content block must have a type key"
|
||||
raise ValueError(msg)
|
||||
if block["type"] in ("reasoning", "function_call") and (
|
||||
not isinstance(message, AIMessage)
|
||||
or message.response_metadata.get("model_provider")
|
||||
!= "anthropic"
|
||||
):
|
||||
continue
|
||||
if block["type"] == "image_url":
|
||||
# convert format
|
||||
source = _format_image(block["image_url"]["url"])
|
||||
|
||||
@@ -2395,6 +2395,26 @@ def test_extras_with_multiple_fields() -> None:
|
||||
assert "input_examples" in tool_def
|
||||
|
||||
|
||||
@pytest.mark.parametrize("block_type", ["reasoning", "function_call"])
|
||||
def test__format_messages_filters_non_anthropic_blocks(block_type: str) -> None:
|
||||
"""Test that reasoning/function_call blocks are filtered for non-anthropic."""
|
||||
block = {"type": block_type, "other": "foo"}
|
||||
human = HumanMessage("hi") # type: ignore[misc]
|
||||
ai = AIMessage( # type: ignore[misc]
|
||||
content=[block, {"type": "text", "text": "hello"}],
|
||||
response_metadata={"model_provider": "openai"},
|
||||
)
|
||||
_, msgs = _format_messages([human, ai])
|
||||
assert msgs[1]["content"] == [{"type": "text", "text": "hello"}]
|
||||
|
||||
ai_anthropic = AIMessage( # type: ignore[misc]
|
||||
content=[block, {"type": "text", "text": "hello"}],
|
||||
response_metadata={"model_provider": "anthropic"},
|
||||
)
|
||||
_, msgs = _format_messages([human, ai_anthropic])
|
||||
assert any(b["type"] == block_type for b in msgs[1]["content"])
|
||||
|
||||
|
||||
def test__format_messages_trailing_whitespace() -> None:
|
||||
"""Test that trailing whitespace is trimmed from the final assistant message."""
|
||||
human = HumanMessage("foo") # type: ignore[misc]
|
||||
|
||||
Reference in New Issue
Block a user