You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This commit was created on GitHub.com and signed with GitHub’s verified signature.
fix: decode document assets by media type (#257)
## Problem
`_send_http_request` (sync and async clients) decided how to decode a
response from an exact match on `Content-Type`:
- `image/jpeg`, `image/png`, `image/webp`, `audio/*` and `video/*` came
back as bytes.
- Only an exact `text/markdown` came back as str.
- Everything else went through `response.json()`.
So `get_document_asset` raised `JSONDecodeError` on any other file type.
That includes `raw` assets (the original upload, stored with
`ENABLE_RAW_FILE_ASSET=true`), whose content type is the upload's own,
such as `application/pdf` or `text/csv`.
It also raised for Markdown assets served by a real Compass. The asset
endpoint returns `fastapi.Response(content, media_type=content_type)`,
and Starlette appends a charset to `text/*` types, so the header is
`text/markdown; charset=utf-8` and never matched exactly.
## Change
The client now strips parameters from the media type before comparing:
| Media type | Result |
|---|---|
| `application/json`, `*+json`, or no content type | `response.json()`,
or `None` when the body is empty (unchanged) |
| `text/*` | `response.text` (str) |
| anything else | `response.content` (bytes) |
The `get_document_asset` docstrings now describe these return types.
Version bumped to 2.27.2.
JSON API responses are decoded as before. I checked that no Compass API
route returns a `text/*` response; `PlainTextResponse` appears only in
tests.
## Testing
- New parametrized `test_get_document_asset_decodes_by_media_type` runs
against both the sync and async clients. It covers Markdown and CSV with
a charset, `text/plain`, JSON with a charset, `+json`, PDF,
`application/octet-stream`, GIF and MP4.
- `poetry run pytest`: 483 passed, 89.6% coverage. ruff and pyright
pass.
Co-authored-by: North Code <noreply@cohere.com>