Skip to content

Follow 303 redirects when the API returns them#106

Merged
delner merged 1 commit intomainfrom
fix/follow_api_redirects
Feb 18, 2026
Merged

Follow 303 redirects when the API returns them#106
delner merged 1 commit intomainfrom
fix/follow_api_redirects

Conversation

@delner
Copy link
Collaborator

@delner delner commented Feb 18, 2026

Fix HTTP 303 redirect handling for large dataset responses

Fixes #101

Problem

The Braintrust API returns HTTP 303 redirects to S3 presigned URLs when responses are large (e.g., dataset fetches via POST /btql). The SDK treated all non-2xx responses as errors, so these redirects raised exceptions like:

HTTP 303 for POST https://api.braintrust.dev/btql: See Other

Solution

Extracted a new Braintrust::Internal::Http module with two composable utilities:

  • Http.with_redirects(uri, request) — drop-in replacement for raw Net::HTTP request execution that follows HTTP redirects. Handles 301/302/303 (follow with GET), 307/308 (preserve method and body), strips Authorization headers on cross-host redirects (e.g., API → S3), and enforces a configurable max redirect limit.

  • Http.decompress_response!(response) — decompresses response bodies based on Content-Encoding (gzip, x-gzip). No-op if uncompressed.

The following HTTP call sites now use with_redirects:

  • API::Datasets — list, get, create, insert, fetch (fetch also calls decompress_response!)
  • API::Functions — list, create, invoke, get, delete
  • API::Internal::Projects — create
  • API::Internal::Experiments — create
  • API::Internal::Auth — login
  • Trace::Attachment — from_url

Redirect handling

Status Before After
301 Moved Permanently Raised Braintrust::Error Follow with GET
302 Found Raised Braintrust::Error Follow with GET
303 See Other Raised Braintrust::Error Follow with GET, strip auth on cross-host
307 Temporary Redirect Raised Braintrust::Error Follow, preserving method and body
308 Permanent Redirect Raised Braintrust::Error Follow, preserving method and body
gzip Content-Encoding Not handled Decompressed transparently

@delner delner merged commit c348977 into main Feb 18, 2026
7 checks passed
@delner delner deleted the fix/follow_api_redirects branch February 18, 2026 04:55
@delner
Copy link
Collaborator Author

delner commented Feb 18, 2026

This was released in v0.1.4.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: using dataset on Braintrust::Eval.run raises Braintrust::Error: HTTP 303 error

2 participants