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
This commit is contained in:
Jacob Lee
2024-06-13 15:08:24 -07:00
committed by GitHub
parent bae82e966a
commit bcbb43480c
3 changed files with 36 additions and 0 deletions

View File

@@ -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(