Skip to content

Comments

fix: return raw binary responses without base64 encoding#17

Merged
justrach merged 2 commits intomainfrom
fix/binary-response-handling
Feb 3, 2026
Merged

fix: return raw binary responses without base64 encoding#17
justrach merged 2 commits intomainfrom
fix/binary-response-handling

Conversation

@justrach
Copy link
Owner

@justrach justrach commented Feb 3, 2026

Summary

  • Fixed binary response handling (audio/wav, image/png, video/mp4, etc.) - now returns raw bytes instead of base64-encoded JSON
  • Changed from downcast::<PyBytes>() to extract::<Vec<u8>>() for reliable binary detection across Python 3.13, 3.13t, and 3.14
  • Added _is_binary_content_type() helper to detect binary media types
  • Added comprehensive tests for binary response handling
  • Bumped version to 0.5.22

Changes

Rust (src/server.rs):

  • All handler paths now check for Response objects with binary body content
  • Extract media_type from Response objects to set correct Content-Type header
  • Use extract::<Vec<u8>>() instead of downcast::<PyBytes>() for cross-version compatibility

Python (python/turboapi/request_handler.py):

  • Added _is_binary_content_type() helper
  • Updated normalize_response() to return content_type as 3rd element
  • Added format_response() for binary content handling

Tests:

  • Added tests/test_binary_responses.py with 21 comprehensive tests
  • Added tests/fixtures/test_audio.wav for integration testing

Test plan

  • All 21 binary response tests pass
  • Existing tests pass (41 total)
  • Manual testing with real WAV file returns correct audio/wav content

justrach and others added 2 commits February 3, 2026 17:05
When returning binary data (audio/wav, image/png, video/mp4, etc.),
TurboAPI was base64 encoding the bytes through JSON serialization.
This fix detects binary content types and returns raw bytes directly.

Changes:
- Add _is_binary_content_type() helper to detect binary media types
- Update normalize_response() to return content_type for Response objects
- Add format_response() method that handles binary content directly
- Update all Rust handler paths (sync, async, enhanced) to:
  - Extract media_type from Response objects
  - Check for PyBytes content and return as raw_body
  - Set proper Content-Type header for binary responses
- Add comprehensive tests for binary response handling
- Update existing tests for 3-tuple return from normalize_response

Generated with AI

Co-Authored-By: AI <ai@example.com>
Changed from downcast::<PyBytes>() to extract::<Vec<u8>>() for more
reliable binary content detection across Python versions (3.13, 3.13t, 3.14).

The downcast method was failing silently on some Python configurations,
causing binary responses to fall through to JSON serialization which
output "<binary content>" placeholder instead of raw bytes.

Bumped version to 0.5.22.

Generated with AI

Co-Authored-By: AI <ai@example.com>
@justrach justrach merged commit a39a0f0 into main Feb 3, 2026
6 of 11 checks passed
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