Skip to content

Conversation

@chris-freeman-glean
Copy link
Contributor

Fix multipart file field names with Speakeasy SDK hook

Problem

Speakeasy generates incorrect code that adds "[]" suffix to file field names in multipart forms:

# Generated (incorrect):
files.append((f_name + "[]", (file_name, content, content_type)))

# Expected:
files.append((f_name, (file_name, content, content_type)))

The "[]" suffix should only apply to regular form field arrays, not file fields.

Solution

Implemented an SDKInitHook that patches serialize_multipart_form at runtime:

  1. Wraps the original function during SDK initialization
  2. Post-processes the files list to remove "[]" suffix from file fields only
  3. Identifies file fields by their tuple structure: (filename, content, [content_type])
  4. Preserves "[]" suffix for regular form field arrays

Files Changed

  • src/glean/api_client/_hooks/multipart_fix_hook.py - Hook implementation
  • src/glean/api_client/_hooks/registration.py - Hook registration
  • tests/test_multipart_fix_hook.py - Test coverage

Testing

Run python -m pytest tests/test_multipart_fix_hook.py -v

Notes

  • Hook runs automatically on SDK initialization
  • Generated code in forms.py still contains the original bug
  • Fix applies at runtime during multipart serialization
  • No breaking changes to public APIs

- Add MultipartFileFieldFixHook that patches serialize_multipart_form at runtime
- Fix issue where Speakeasy generates incorrect '[]' suffix on file field names
- Hook removes '[]' suffix only from file fields, preserves it for form arrays
- Add comprehensive tests covering file field detection and patching logic

Fixes multipart form uploads by intercepting serialization before HTTP requests.
The hook survives Speakeasy code regeneration since it patches at SDK init time.
- Move forms import to module level to allow proper test patching
- Fix _is_file_field_data method to exclude empty strings and None values
- Add filename validation to distinguish JSON content from file content
- All tests now pass including edge cases for file field detection
@chris-freeman-glean chris-freeman-glean force-pushed the cfreeman/fix-multipart-form-bug branch from 1f308b0 to 643f273 Compare August 14, 2025 18:47
@chris-freeman-glean chris-freeman-glean marked this pull request as ready for review August 18, 2025 20:35
@chris-freeman-glean chris-freeman-glean requested a review from a team as a code owner August 18, 2025 20:35
@rwjblue-glean rwjblue-glean merged commit 0123565 into main Aug 29, 2025
3 checks passed
@rwjblue-glean rwjblue-glean deleted the cfreeman/fix-multipart-form-bug branch August 29, 2025 15:45
@rwjblue-glean rwjblue-glean added the bug Something isn't working label Aug 29, 2025
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.

3 participants