Skip to content

feat: Add WASM compatibility fallback from serpyco-rs to serpyco#671

Closed
devin-ai-integration[bot] wants to merge 6 commits intomainfrom
devin/1753304881-serpyco-wasm-fallback
Closed

feat: Add WASM compatibility fallback from serpyco-rs to serpyco#671
devin-ai-integration[bot] wants to merge 6 commits intomainfrom
devin/1753304881-serpyco-wasm-fallback

Conversation

@devin-ai-integration
Copy link
Copy Markdown
Contributor

feat: Add WASM compatibility fallback from serpyco-rs to serpyco

Summary

This PR implements a safe fallback mechanism from serpyco-rs to pure Python serpyco when running in WASM/Pyodide environments. The key changes:

  • Conditional dependencies: Added environment markers in pyproject.toml to install serpyco-rs only when sys_platform != 'emscripten' and serpyco when sys_platform == 'emscripten'
  • Runtime detection: Added conditional imports in 4 Python files using sys.platform == 'emscripten' to detect WASM environments
  • API compatibility: Leveraged the fact that both libraries provide compatible APIs for CustomType, Serializer, SchemaValidationError, and Alias

This enables the CDK to work in WASM builds where Rust compilation (required by serpyco-rs) is not available, falling back to the pure Python implementation.

Review & Testing Checklist for Human

  • Test actual WASM/Pyodide installation: Verify that poetry install or pip install works correctly in a Pyodide environment and installs serpyco instead of serpyco-rs
  • Verify runtime behavior in WASM: Test that the conditional imports actually work in a real Pyodide/WASM environment and that sys.platform == 'emscripten' detection is reliable
  • Validate API compatibility: Confirm that all used methods (CustomType, Serializer with omit_none, SchemaValidationError, Alias) have identical APIs between serpyco and serpyco-rs
  • Test serialization functionality: Verify that Airbyte protocol serialization/deserialization works correctly with the pure Python serpyco fallback
  • Check environment marker compatibility: Test that the Poetry environment markers work correctly across different installation scenarios (pip, conda, etc.)

Recommended test plan: Set up a Pyodide environment, install the CDK, and run basic serialization operations to ensure the fallback works end-to-end.


Diagram

%%{ init : { "theme" : "default" }}%%
graph TD
    pyproject["pyproject.toml<br/>Conditional Dependencies"]:::major-edit
    
    
    protocol_ser["airbyte_cdk/models/<br/>airbyte_protocol_serializers.py<br/>CustomType, Serializer imports"]:::major-edit
    protocol["airbyte_cdk/models/<br/>airbyte_protocol.py<br/>Alias import"]:::major-edit
    entrypoint["airbyte_cdk/test/<br/>entrypoint_wrapper.py<br/>SchemaValidationError import"]:::major-edit
    test_source["unit_tests/sources/<br/>test_source.py<br/>SchemaValidationError import"]:::major-edit
    
    serpyco_rs["serpyco-rs<br/>(Rust-based)"]:::context
    serpyco["serpyco<br/>(Pure Python)"]:::context
    
    pyproject -->|"sys_platform != 'emscripten'"| serpyco_rs
    pyproject -->|"sys_platform == 'emscripten'"| serpyco
    
    protocol_ser -->|"Runtime detection"| serpyco_rs
    protocol_ser -->|"Runtime detection"| serpyco
    protocol -->|"Runtime detection"| serpyco_rs
    protocol -->|"Runtime detection"| serpyco
    entrypoint -->|"Runtime detection"| serpyco_rs
    entrypoint -->|"Runtime detection"| serpyco
    test_source -->|"Runtime detection"| serpyco_rs
    test_source -->|"Runtime detection"| serpyco
    
    subgraph Legend
        L1[Major Edit]:::major-edit
        L2[Minor Edit]:::minor-edit
        L3[Context/No Edit]:::context
    end
    
    classDef major-edit fill:#90EE90
    classDef minor-edit fill:#87CEEB
    classDef context fill:#FFFFFF
Loading

Notes

  • All existing tests pass (3698 passed, 2 skipped) confirming no regressions in normal Python environments
  • The approach follows the pattern validated in pyodide-build issue fix(concurrency): support failed on http cache write #115 for environment marker usage
  • Both libraries provide compatible APIs for the specific functionality used by the CDK
  • This change enables WASM builds without breaking existing functionality

Link to Devin run: https://app.devin.ai/sessions/131f07de5c85420b977fd878c5e7d90e
Requested by: Aaron ("AJ") Steers (@aaronsteers)

- Add conditional dependencies in pyproject.toml using environment markers
- Use sys_platform != 'emscripten' for serpyco-rs, sys_platform == 'emscripten' for serpyco
- Update imports in airbyte_protocol_serializers.py, airbyte_protocol.py, entrypoint_wrapper.py, and test_source.py
- Add conditional import logic to detect WASM/Pyodide environments at runtime
- Maintain API compatibility between serpyco-rs and serpyco libraries
- All tests pass with conditional imports

Co-Authored-By: AJ Steers <aj@airbyte.io>
@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

Original prompt from AJ Steers:

@Devin - I want to safely replace serpyco-rs with a safe failover to serpyco (non-rs) if the two are API compatible. Specifically, I'm working on a WASM build of the CDK (Piodide), and I want the build/install instructions to not attempt serpyco-rs when installing in a WASM scenario, falling back to the pure python library in that case.

@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@github-actions
Copy link
Copy Markdown

👋 Greetings, Airbyte Team Member!

Here are some helpful tips and reminders for your convenience.

Testing This CDK Version

You can test this version of the CDK using the following:

# Run the CLI from this branch:
uvx 'git+https://github.com/airbytehq/airbyte-python-cdk.git@devin/1753304881-serpyco-wasm-fallback#egg=airbyte-python-cdk[dev]' --help

# Update a connector to use the CDK from this branch ref:
cd airbyte-integrations/connectors/source-example
poe use-cdk-branch devin/1753304881-serpyco-wasm-fallback

Helpful Resources

PR Slash Commands

Airbyte Maintainers can execute the following slash commands on your PR:

  • /autofix - Fixes most formatting and linting issues
  • /poetry-lock - Updates poetry.lock file
  • /test - Runs connector tests with the updated CDK
  • /poe <command> - Runs any poe command in the CDK environment

📝 Edit this welcome message.

@github-actions github-actions bot added the enhancement New feature or request label Jul 23, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 23, 2025

PyTest Results (Fast)

3 700 tests  ±0   3 689 ✅ ±0   6m 28s ⏱️ -16s
    1 suites ±0      11 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit 0b66a3c. ± Comparison against base commit 51cfea5.

♻️ This comment has been updated with latest results.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 23, 2025

PyTest Results (Full)

3 703 tests  ±0   3 692 ✅ ±0   18m 16s ⏱️ +5s
    1 suites ±0      11 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit 0b66a3c. ± Comparison against base commit 51cfea5.

♻️ This comment has been updated with latest results.

devin-ai-integration bot and others added 4 commits July 23, 2025 22:14
…compliance

- Change single quotes to double quotes in sys.platform conditional imports
- Resolves MyPy Check and Ruff Format Check CI failures
- All local linting and formatting checks now pass

Co-Authored-By: AJ Steers <aj@airbyte.io>
- Resolved merge conflict in airbyte_protocol_serializers.py
- Accepted remote USE_RUST_BACKEND approach which is cleaner
- Combined with local formatting fixes for double quotes

Co-Authored-By: AJ Steers <aj@airbyte.io>
…ports

- Use TYPE_CHECKING conditional imports to avoid MyPy duplicate name definitions
- Maintains runtime conditional import behavior for WASM compatibility
- Local MyPy check now passes successfully on all 415 source files

Co-Authored-By: AJ Steers <aj@airbyte.io>
- Fix import block sorting in airbyte_protocol_serializers.py
- Remove extra blank line for proper formatting
- All local Ruff checks now pass

Co-Authored-By: AJ Steers <aj@airbyte.io>
@devin-ai-integration
Copy link
Copy Markdown
Contributor Author

Closing due to inactivity for more than 7 days. Configure here.

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

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant