Skip to content

Commit 231d352

Browse files
Map malformed 200 body to ProviderInvalidResponse in skeleton
The authoring-guide embed() skeleton parsed the 200 response body without guarding it, so a malformed body would leak a raw KeyError/ValueError instead of the ProviderInvalidResponse its own contract checklist prescribes. Wrap the parse (PR #220 review).
1 parent 362f3af commit 231d352

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

docs/retrieval-providers/authoring.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,15 @@ class MyEmbeddingProvider:
7070
if resp.status_code != 200:
7171
raise _classify(resp)
7272

73-
payload = resp.json()
74-
# Order the returned data by index so vectors line up with input.
75-
entries = sorted(payload["data"], key=lambda e: e["index"])
76-
vectors = [[float(x) for x in e["embedding"]] for e in entries]
73+
# A malformed 200 body (bad JSON, missing keys, wrong types) is a
74+
# provider_invalid_response, not a leaked KeyError/ValueError.
75+
try:
76+
payload = resp.json()
77+
# Order the returned data by index so vectors line up with input.
78+
entries = sorted(payload["data"], key=lambda e: e["index"])
79+
vectors = [[float(x) for x in e["embedding"]] for e in entries]
80+
except (ValueError, KeyError, TypeError) as exc:
81+
raise ProviderInvalidResponse("malformed embeddings response") from exc
7782
# Validates the count and uniform dimensionality, returns the dim.
7883
dimensions = validate_embedding_response(vectors, len(input_strings))
7984

0 commit comments

Comments
 (0)