mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
fix(openai): handle missing 'text' key in responses API content blocks (#34198)
This commit is contained in:
@@ -4048,9 +4048,14 @@ def _construct_responses_api_input(messages: Sequence[BaseMessage]) -> list:
|
|||||||
if block_type in ("text", "output_text", "refusal"):
|
if block_type in ("text", "output_text", "refusal"):
|
||||||
msg_id = block.get("id")
|
msg_id = block.get("id")
|
||||||
if block_type in ("text", "output_text"):
|
if block_type in ("text", "output_text"):
|
||||||
|
# Defensive check: block may not have "text" key
|
||||||
|
text = block.get("text")
|
||||||
|
if text is None:
|
||||||
|
# Skip blocks without text content
|
||||||
|
continue
|
||||||
new_block = {
|
new_block = {
|
||||||
"type": "output_text",
|
"type": "output_text",
|
||||||
"text": block["text"],
|
"text": text,
|
||||||
"annotations": [
|
"annotations": [
|
||||||
_format_annotation_from_lc(annotation)
|
_format_annotation_from_lc(annotation)
|
||||||
for annotation in block.get("annotations") or []
|
for annotation in block.get("annotations") or []
|
||||||
|
|||||||
@@ -2091,6 +2091,38 @@ def test__construct_responses_api_input_multiple_message_components() -> None:
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test__construct_responses_api_input_skips_blocks_without_text() -> None:
|
||||||
|
"""Test that blocks without 'text' key are skipped."""
|
||||||
|
# Test case: block with type "text" but missing "text" key
|
||||||
|
messages = [
|
||||||
|
AIMessage(
|
||||||
|
content=[
|
||||||
|
{"type": "text", "text": "valid text", "id": "msg_123"},
|
||||||
|
{"type": "text", "id": "msg_123"}, # Missing "text" key
|
||||||
|
{"type": "output_text", "text": "valid output", "id": "msg_123"},
|
||||||
|
{"type": "output_text", "id": "msg_123"}, # Missing "text" key
|
||||||
|
]
|
||||||
|
)
|
||||||
|
]
|
||||||
|
result = _construct_responses_api_input(messages)
|
||||||
|
|
||||||
|
# Should only include blocks with valid text content
|
||||||
|
assert len(result) == 1
|
||||||
|
assert result[0]["type"] == "message"
|
||||||
|
assert result[0]["role"] == "assistant"
|
||||||
|
assert len(result[0]["content"]) == 2
|
||||||
|
assert result[0]["content"][0] == {
|
||||||
|
"type": "output_text",
|
||||||
|
"text": "valid text",
|
||||||
|
"annotations": [],
|
||||||
|
}
|
||||||
|
assert result[0]["content"][1] == {
|
||||||
|
"type": "output_text",
|
||||||
|
"text": "valid output",
|
||||||
|
"annotations": [],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def test__construct_responses_api_input_human_message_with_image_url_conversion() -> (
|
def test__construct_responses_api_input_human_message_with_image_url_conversion() -> (
|
||||||
None
|
None
|
||||||
):
|
):
|
||||||
|
|||||||
Reference in New Issue
Block a user