fix(fireworks): strip non-wire keys from ToolMessage text content blocks (#37187)

Fireworks's chat completions endpoint rejects unknown fields on tool
message content blocks — specifically the `id` key that LangChain
auto-generates on `TextContentBlock`. Add
`_sanitize_chat_completions_content` to strip those extra keys before
the payload hits the wire, preventing `Extra inputs are not permitted`
errors on tool message round-trips.
This commit is contained in:
Mason Daugherty
2026-05-05 11:10:55 -04:00
committed by GitHub
parent afa7e992ef
commit 4498d3dc84
2 changed files with 49 additions and 1 deletions

View File

@@ -31,6 +31,7 @@ from langchain_fireworks.chat_models import (
_convert_dict_to_message,
_convert_message_to_dict,
_format_message_content,
_sanitize_chat_completions_content,
_usage_to_metadata,
)
@@ -106,6 +107,32 @@ def test_format_message_content_passthrough_string() -> None:
assert _format_message_content("hello") == "hello"
def test_sanitize_chat_completions_text_blocks_strips_id() -> None:
"""LangChain auto-generated `id` on text blocks must not reach the wire.
Fireworks's chat completions schema rejects unknown keys on tool message
content blocks (`Extra inputs are not permitted, ... [0].id`).
"""
message = ToolMessage(
content=[{"type": "text", "text": "foo", "id": "lc_abc123"}],
tool_call_id="def456",
)
assert _convert_message_to_dict(message) == {
"role": "tool",
"content": [{"type": "text", "text": "foo"}],
"tool_call_id": "def456",
}
def test_sanitize_chat_completions_content_passthrough_string() -> None:
assert _sanitize_chat_completions_content("hello") == "hello"
def test_sanitize_chat_completions_content_passthrough_non_text_block() -> None:
blocks = [{"type": "image_url", "image_url": {"url": "https://x/y.png"}}]
assert _sanitize_chat_completions_content(blocks) == blocks
def test_format_message_content_translates_v1_image_block() -> None:
"""Canonical v1 image block is translated to OpenAI image_url + data URI."""
blocks = [{"type": "image", "base64": "abc", "mime_type": "image/png"}]