fix: return raw binary responses without base64 encoding#17
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
downcast::<PyBytes>()toextract::<Vec<u8>>()for reliable binary detection across Python 3.13, 3.13t, and 3.14_is_binary_content_type()helper to detect binary media typesChanges
Rust (
src/server.rs):media_typefrom Response objects to set correct Content-Type headerextract::<Vec<u8>>()instead ofdowncast::<PyBytes>()for cross-version compatibilityPython (
python/turboapi/request_handler.py):_is_binary_content_type()helpernormalize_response()to return content_type as 3rd elementformat_response()for binary content handlingTests:
tests/test_binary_responses.pywith 21 comprehensive teststests/fixtures/test_audio.wavfor integration testingTest plan