core[patch]: Treat type as a special field when merging lists (#22750)

Should we even log a warning? At least for Anthropic, it's expected to
get e.g. `text_block` followed by `text_delta`.

@ccurme @baskaryan @efriis
pull/22868/head
Jacob Lee 4 months ago committed by GitHub
parent bae82e966a
commit bcbb43480c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -105,6 +105,9 @@ def merge_content(
# Add the second content to the last element
if isinstance(first_content[-1], str):
return first_content[:-1] + [first_content[-1] + second_content]
# If second content is an empty string, treat as a no-op
elif second_content == "":
return first_content
else:
# Otherwise, add the second content as a new element of the list
return first_content + [second_content]

@ -60,6 +60,10 @@ def merge_lists(left: Optional[List], right: Optional[List]) -> Optional[List]:
if e_left["index"] == e["index"]
]
if to_merge:
# If a top-level "type" has been set for a chunk, it should no
# longer be overridden by the "type" field in future chunks.
if "type" in merged[to_merge[0]] and "type" in e:
e.pop("type")
merged[to_merge[0]] = merge_dicts(merged[to_merge[0]], e)
else:
merged = merged + [e]

@ -203,6 +203,35 @@ def test_complex_ai_message_chunks() -> None:
)
), "Concatenating when both content arrays are dicts with separate indexes should not merge" # noqa: E501
assert (
AIMessageChunk(content=[{"index": 0, "text": "I am", "type": "text_block"}])
+ AIMessageChunk(
content=[{"index": 0, "text": " indeed.", "type": "text_block"}]
)
== AIMessageChunk(
content=[{"index": 0, "text": "I am indeed.", "type": "text_block"}]
)
), "Concatenating when both content arrays are dicts with the same index and type should merge" # noqa: E501
assert (
AIMessageChunk(content=[{"index": 0, "text": "I am", "type": "text_block"}])
+ AIMessageChunk(
content=[{"index": 0, "text": " indeed.", "type": "text_block_delta"}]
)
== AIMessageChunk(
content=[{"index": 0, "text": "I am indeed.", "type": "text_block"}]
)
), "Concatenating when both content arrays are dicts with the same index and different types should merge without updating type" # noqa: E501
assert (
AIMessageChunk(content=[{"index": 0, "text": "I am", "type": "text_block"}])
+ AIMessageChunk(content="", response_metadata={"extra": "value"})
== AIMessageChunk(
content=[{"index": 0, "text": "I am", "type": "text_block"}],
response_metadata={"extra": "value"},
)
), "Concatenating when one content is an array and one is an empty string should not add a new item, but should concat other fields" # noqa: E501
def test_function_message_chunks() -> None:
assert FunctionMessageChunk(

Loading…
Cancel
Save