Skip to content

Commit 482ab2a

Browse files
fix(utils): guard against None fields in merge_embed_responses (#771)
When batching embed requests, later responses may have None for an embedding field that the first response has set. The inner comprehension now uses `(getattr(..., field) or [])` so None fields are treated as empty rather than raising TypeError: 'NoneType' object is not iterable. Fixes #770 Co-authored-by: Aegis Dev <devteamaegis@users.noreply.github.com> Co-authored-by: fern-support <126544928+fern-support@users.noreply.github.com>
1 parent 3b49748 commit 482ab2a

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/cohere/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def merge_embed_responses(responses: typing.List[EmbedResponse]) -> EmbedRespons
228228
field: [
229229
embedding
230230
for embedding_by_type in embeddings_by_type
231-
for embedding in getattr(embedding_by_type, field)
231+
for embedding in (getattr(embedding_by_type, field) or [])
232232
]
233233
for field in fields
234234
}

tests/test_embed_utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,16 @@ def test_merge_embeddings_floats(self) -> None:
189189
)
190190
))
191191

192+
def test_merge_embeddings_by_type_with_none_field_in_later_response(self) -> None:
193+
resp1 = EmbeddingsByTypeEmbedResponse(
194+
response_type="embeddings_by_type", id="1",
195+
embeddings=EmbedByTypeResponseEmbeddings(float_=[[1.0, 2.0]]))
196+
resp2 = EmbeddingsByTypeEmbedResponse(
197+
response_type="embeddings_by_type", id="2",
198+
embeddings=EmbedByTypeResponseEmbeddings(float_=None))
199+
result = merge_embed_responses([resp1, resp2])
200+
self.assertEqual(result.embeddings.float_, [[1.0, 2.0]]) # type: ignore
201+
192202
def test_sum_fields_if_not_none_with_none_entries(self) -> None:
193203
# billed_units list may contain None when ApiMeta.billed_units is unset;
194204
# sum_fields_if_not_none must skip None objects without raising AttributeError

0 commit comments

Comments
 (0)