Skip to content

fix(postgrest)!: return csv() results as a TextAPIResponse - #2051

Merged
spydon merged 3 commits into
mainfrom
lukasklingsbo/sdk-1706-postgrest-csv-response
Nov 13, 2026
Merged

spydon merged 3 commits into
mainfrom
lukasklingsbo/sdk-1706-postgrest-csv-response

Conversation

@spydon

@spydon spydon commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What

csv() results now have their own response type, TextAPIResponse, instead of being forced into APIResponse.data (typed as a list of rows) or returned as a bare str.

  • TextAPIResponse holds the CSV body as a str in data and the row count in count when the query asked for one. It is exported from postgrest.
  • TextRequestBuilder.execute() returns TextAPIResponse. Before, it returned a bare str and dropped the count from Content-Range.
  • rpc(...).csv() returns the same TextRequestBuilder as csv() on a table. Since fix(postgrest)!: parse responses with json.loads and return scalar RPC results #2049 it returned the text in RPCResponse.data, typed as JSON. That special case in RPCFilterRequestBuilder.execute() is removed, and the RPCResponse docstring and MIGRATION.md now point to TextAPIResponse.
  • csv() moved from BaseRPCRequestBuilder to RPCFilterRequestBuilder, and sdk-compliance.yaml now lists the new symbols.
  • A CSV body that is not valid UTF-8 raises APIError with the response, like an invalid JSON body does.
  • Two rpc() overloads now return the builder rpc() actually returns. count=..., head=True returns RPCCountRequestBuilder (it claimed RPCFilterRequestBuilder), and count=..., get=False returns RPCFilterRequestBuilder (it claimed a union with RPCCountRequestBuilder). Without this, .csv() after those calls would have been accepted by the type checker and failed at runtime, or rejected although it works.

Breaking changes

  • .csv().execute() returns TextAPIResponse instead of str. Read the text from response.data.
  • Filters, modifiers and strip_nulls() can no longer follow rpc(...).csv(). They go before csv(), as they already did on a table. strip_nulls() after csv() already raised ValueError.

MIGRATION.md has a new section with a type-checked example.

Tests

  • New tests/test_text_response.py covers table csv() with a count, sync and async RPC csv(), a non-UTF-8 body, and the corrected rpc() overload types (with assert_type) against a mock transport.
  • Added TextAPIResponse.from_http_request_response unit tests to the sync and async request builder tests.
  • The CSV integration tests now assert on res.data.
  • Removed the RPC csv() then strip_nulls() test, since that chain no longer type-checks. The PostgrestRequest test still covers the ValueError.

Linear: SDK-1706

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • New Features
    • CSV query results now provide the text data and an optional record count in a structured response.
    • RPC queries can now request CSV output, with the response text available through the same interface.
  • Documentation
    • Updated the migration guide with details about CSV response data and count handling, and clarified the ordering guidance for strip_nulls().

CSV responses now return a TextAPIResponse with the response text in data and an optional row count in count. TextRequestBuilder.execute() returns this response object. RPCFilterRequestBuilder.csv() configures CSV output for RPC requests. The package exports TextAPIResponse, and the tests and migration guide reflect the response shape.

Sequence Diagram(s)

sequenceDiagram
  participant RPCFilterRequestBuilder
  participant TextRequestBuilder
  participant PostgREST
  participant TextAPIResponse
  RPCFilterRequestBuilder->>TextRequestBuilder: csv() configures CSV response shape
  TextRequestBuilder->>PostgREST: execute CSV request
  PostgREST-->>TextRequestBuilder: CSV body and response headers
  TextRequestBuilder->>TextAPIResponse: Parse body and optional count
Loading

Change: Bug fix

Merge Risk: Minimal · up to 26685

CSV results now come back as a response object, with the text in data and the row count in count. This is a documented breaking change with a migration note. No merge-blocking risk was found.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: efff0006-174f-41f8-bab2-f8dddda86e87
Commits

Reviewing files that changed from the base of the PR and between 604d8ec and 26685a4.

Files selected for processing: 6

  • MIGRATION.md
  • sdk-compliance.yaml
  • src/postgrest/src/postgrest/__init__.py
  • src/postgrest/src/postgrest/client.py
  • src/postgrest/src/postgrest/request_builder.py
  • src/postgrest/tests/test_rpc_query_params.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 0 remain after this review.

csv() results get their own response type, TextAPIResponse, with the body as a str in data and the requested row count in count. rpc(...).csv() now returns the same TextRequestBuilder as csv() on a table instead of parsing the CSV body as JSON.

SDK-1706
@spydon
spydon force-pushed the lukasklingsbo/sdk-1706-postgrest-csv-response branch from 604d8ec to 26685a4 Compare November 13, 2026 16:29
@spydon
spydon merged commit fd9ff93 into main Nov 13, 2026
28 checks passed
@spydon
spydon deleted the lukasklingsbo/sdk-1706-postgrest-csv-response branch November 13, 2026 16:45
spydon added a commit that referenced this pull request Nov 16, 2026
…er (#2053)

## What

`rpc(..., count=...)` without `head` is typed as
`RPCFilterRequestBuilder`, so `rpc(..., count=...).csv()` type-checks.
It also adds the integration and type-check coverage SDK-2975 asked for.
#2051 already made `rpc().csv()` return a `TextRequestBuilder`.

## Changes

- The `rpc()` overloads of `PostgrestClient` are reduced to the three
`SupabaseClient` uses: `head=False` (the default) gives
`RPCFilterRequestBuilder`, `head=True` gives `RPCCountRequestBuilder`,
and a plain `bool` gives the union. Before, a call with only `count=`
fell through to the catch-all overload and was typed as the union, which
has no `csv()`.
- New type-check fixture `tests/typecheck/rpc.py`, checked with mypy,
pyright and pyrefly: `rpc(...).csv()` and its `TextAPIResponse` for both
clients, `count=` with and without `head=True`, and the rejected chains
after `csv()` and `csv()` on the count builder.
- Integration tests in `tests/_async` and `tests/_sync` that call a
function with `.csv()` against the local stack.

Fixes SDK-2975
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.

1 participant