Skip to content

fix(utils): embed(texts=[]) crashes with IndexError on empty input#778

Open
devteamaegis wants to merge 1 commit into
cohere-ai:mainfrom
devteamaegis:fix/embed-empty-list
Open

fix(utils): embed(texts=[]) crashes with IndexError on empty input#778
devteamaegis wants to merge 1 commit into
cohere-ai:mainfrom
devteamaegis:fix/embed-empty-list

Conversation

@devteamaegis

@devteamaegis devteamaegis commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

What's broken

Client.embed(texts=[]) (default batching, no images) produces an empty responses list, which merge_embed_responses indexes unconditionally:

>>> import cohere
>>> co = cohere.Client(api_key="fake-no-network")
>>> co.embed(texts=[])
IndexError: list index out of range   # src/cohere/utils.py, responses[0].response_type

The crash happens before any network I/O, so an empty input list never gets a meaningful error.

Why it happens

merge_embed_responses reads responses[0] without checking for an empty list.

Fix

Guard the empty case and raise a clear ValueError. The SDK can't synthesise a typed empty EmbedResponse (it wouldn't know response_type), so an explicit, actionable error is the smallest correct behavior and matches the library's existing input-validation pattern.

Test

Added a case asserting merge_embed_responses([]) raises ValueError instead of IndexError.

Fixes #777


Note

Low Risk
Small input-validation guard and test only; no API or embedding merge logic changes for non-empty input.

Overview
Fixes Client.embed(texts=[]) (and any path that merges zero batched responses) so it no longer crashes with IndexError when merge_embed_responses accessed responses[0].

merge_embed_responses now rejects an empty responses list up front with a ValueError and an explicit message that no texts were provided. A unit test locks in that behavior instead of an IndexError.

Reviewed by Cursor Bugbot for commit e7ad74b. Bugbot is set up for automated code reviews on this repo. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client.embed(texts=[]) raises IndexError instead of handling empty input

1 participant