Skip to content

Rust bridge: class methods, unbounded generics, and host callbacks - #4093

Merged
2kai2kai2 merged 10 commits into
canaryfrom
kai/rust-bridge
Jul 21, 2026
Merged

2kai2kai2 merged 10 commits into
canaryfrom
kai/rust-bridge

Conversation

@2kai2kai2

@2kai2kai2 2kai2kai2 commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor
  • Adds class method bindings (both static and instance). Like other bridges, these copy-in self but have no copy-out so mutations are not reflected in the host value.
  • Adds unbounded generics (items that rely on interface-bounded generics are currently omitted) for functions, methods, and classes, including generated enums for unions containing type args
  • Adds host callback + host value handle mapping, allowing a typed rust callback to be passed into a BAML function and executed correctly. They may raise host errors which are opaquely passed through BAML as a handle and can be rehydrated when a handle is passed back to BAML.
  • Codegen for SDKs now correctly provides the implicit generic parameters created by function-typed parameters with no explicit throws clause. While type-erased in Python/TS due to their type safety limitations, Rust can generate and infer them in generic positions.

Summary by CodeRabbit

  • New Features
    • Expanded Rust SDK support for generics across classes, functions, methods, and unions, including supplying explicit type arguments in calls.
    • Added a Rust host-value bridge module for host callables and opaque host errors, with improved callback-throws behavior.
  • Bug Fixes
    • Improved Rust throws/callback-throws inference, including correct catch/rethrow error surfaces and typed vs opaque host error decoding.
  • Documentation
    • Updated generated Rust docs to consistently use an # Errors section for throwing APIs.
  • Tests
    • Significantly expanded/updated Rust SDK tests and fixtures for generics, host callables, and throws/callback-throws behavior.

@vercel

vercel Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
beps Ready Ready Preview, Comment Jul 21, 2026 7:06pm
promptfiddle Ready Ready Preview, Comment Jul 21, 2026 7:06pm
promptfiddle2 Ready Ready Preview, Comment Jul 21, 2026 7:06pm

Request Review

@github-actions

Copy link
Copy Markdown

⏭️ Performance benchmarks were skipped

Perf benchmarks (CodSpeed) are opt-in on pull requests — they no longer run on every push. They always run automatically after merge to canary/main.

To run them on this PR, do any of the following, then push a commit (or re-run CI):

  • Add RUN_CODSPEED=1 to the PR description, or
  • Include run-perf or /perf in the PR title or any commit message.

@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 1f4ce597-aeab-4c99-9905-e918aeab8735

📥 Commits

Reviewing files that changed from the base of the PR and between 36786b4 and eedc99f.

📒 Files selected for processing (1)
  • baml_language/sdks/rust/bridge_rust/src/host_value.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • baml_language/sdks/rust/bridge_rust/src/host_value.rs

📝 Walkthrough

Walkthrough

This PR extends Rust SDK code generation and runtime support for generic types, generated methods, callback effect inference, host-callable dispatch, typed and opaque errors, and explicit wire type arguments. It also updates symbol traits, generated documentation, test fixtures, and Rust test gating.

Changes

Rust SDK code generation

Layer / File(s) Summary
Signature lowering and effect parameters
baml_language/crates/baml_codegen_types/src/symbols.rs, baml_language/crates/baml_project/src/client_codegen.rs, baml_language/sdks/rust/sdkgen_rust/src/effect_rename.rs
Symbol types derive Clone; function and method signatures use elaborated HIR data, and synthesized callback effect parameters are renamed before emission.
Generic analysis and generated bindings
baml_language/sdks/rust/sdkgen_rust/src/{analyze.rs,translate_ty.rs,unions.rs}, baml_language/sdks/rust/sdkgen_rust/src/emit/*
Generic classes, TypeVars, generic unions, class methods, callback parameters, type metadata, and Rust # Errors documentation are emitted.
Wire metadata and invocation arguments
baml_language/sdks/rust/bridge_rust/src/{baml_value.rs,encode.rs,runtime.rs,capi.rs}
Rust values provide wire BamlTy descriptors; class encoders and runtime invocations carry explicit generic type arguments, with host-dispatch ABI entries added.
Host-callable bridge
baml_language/sdks/rust/bridge_rust/src/{host_value.rs,error.rs,lib.rs}
Callable handles, sync/async dispatch, optional argument slotting, typed throws, opaque host errors, rehydration, and related public APIs are implemented.
SDK and bridge validation
baml_language/sdk_tests/**, baml_language/sdks/rust/bridge_rust/tests/*, baml_language/sdk_tests/harness_setup/src/rust.rs
Tests cover generic calls, generic round trips, host-callable behavior, callback-throws inference, error documentation, and newly enabled Rust test ports.

Estimated code review effort: 5 (Critical) | ~120 minutes

Sequence Diagram(s)

sequenceDiagram
  participant GeneratedBinding
  participant RustRuntime
  participant BAML_Engine
  participant HostCallableBridge
  GeneratedBinding->>RustRuntime: invoke with kwargs and type_args
  RustRuntime->>BAML_Engine: dispatch CallFunctionArgs
  BAML_Engine->>HostCallableBridge: dispatch registered callable
  HostCallableBridge->>HostCallableBridge: decode args and run closure
  HostCallableBridge->>BAML_Engine: complete host call with result or throw
Loading

Possibly related PRs

  • BoundaryML/baml#3571: The Rust bridge work builds on the host-value handle, dispatch, and HostCallable error plumbing introduced by this PR.
  • BoundaryML/baml#4033: The Rust bridge runtime and call envelope changes modify the same type-argument and bridge APIs.
  • BoundaryML/baml#4087: The client-codegen signature-lowering refactor modifies the same function and method lowering pipeline.

Poem

A rabbit hops through types so bright,
Generic carrots take their flight.
Callbacks whisper, errors gleam,
Handles cross the BAML stream.
Rust docs bloom with # Errors true—
A fluffy bridge now carries through!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is concise and accurately captures the PR’s main additions: class methods, generics, and host callbacks.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch kai/rust-bridge

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 20, 2026 23:32 Inactive
@github-actions

github-actions Bot commented Jul 20, 2026 •

Copy link
Copy Markdown

Binary size checks passed

✅ 7 passed

Artifact Platform File Gzip Gated on Baseline Delta Status
✅ baml-cli Linux 🔒 25.5 MB 10.8 MB file 25.3 MB +218.5 KB (+0.9%) OK
✅ packed-program Linux 🔒 17.0 MB 7.0 MB file 17.0 MB +4.3 KB (+0.0%) OK
✅ baml-cli macOS 🔒 19.7 MB 9.4 MB file 19.5 MB +198.6 KB (+1.0%) OK
✅ packed-program macOS 🔒 13.2 MB 6.2 MB file 13.2 MB +16.7 KB (+0.1%) OK
✅ baml-cli Windows 🔒 21.3 MB 9.6 MB file 21.1 MB +206.8 KB (+1.0%) OK
✅ packed-program Windows 🔒 14.2 MB 6.2 MB file 14.1 MB +11.0 KB (+0.1%) OK
✅ bridge_wasm WASM 16.2 MB 🔒 4.4 MB gzip 4.4 MB -504 B (-0.0%) OK

🔒 = the size this artifact is GATED on (ceiling + delta). Binaries gate on file size (installed binary); WASM gates on gzip (download size). The other size is shown for information only.


Generated by cargo size-gate · workflow run

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
baml_language/sdks/rust/bridge_rust/src/host_value.rs (1)

117-124: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider adding #[cfg(test)] unit tests for these pure helpers.

next_key/slot_args are pure, easily unit-testable (positional/named slotting, missing-required-arg error, wraparound-skip-zero). Per coding guidelines, prefer unit tests over integration tests where possible; today coverage for this logic lives only in the integration tests under bridge_rust/tests/conversions.rs.

As per coding guidelines: "Prefer writing Rust unit tests over integration tests where possible."

Also applies to: 556-588

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@baml_language/sdks/rust/bridge_rust/src/host_value.rs` around lines 117 -
124, Add a #[cfg(test)] unit-test module in host_value.rs covering the pure
helpers next_key and slot_args: verify positional and named slotting,
missing-required-argument errors, and next_key’s wraparound behavior skipping
zero. Keep the tests focused on these helpers and use existing test conventions
and symbols.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@baml_language/sdks/rust/bridge_rust/src/host_value.rs`:
- Around line 247-268: Update the closure invocation in the outer DISPATCH_RT
task to run through handle.spawn_blocking, await its JoinHandle, and preserve
the existing success, argument-decode failure, and panic/error completion paths.
Ensure the sync closure body executes off the async dispatch workers and that
any spawn_blocking JoinError calls complete_bridge_failure so every in-flight
call is completed.

---

Nitpick comments:
In `@baml_language/sdks/rust/bridge_rust/src/host_value.rs`:
- Around line 117-124: Add a #[cfg(test)] unit-test module in host_value.rs
covering the pure helpers next_key and slot_args: verify positional and named
slotting, missing-required-argument errors, and next_key’s wraparound behavior
skipping zero. Keep the tests focused on these helpers and use existing test
conventions and symbols.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: b73af529-f218-4ae1-8f14-fff813e04478

📥 Commits

Reviewing files that changed from the base of the PR and between 68da999 and 03212bd.

📒 Files selected for processing (31)
  • baml_language/crates/baml_codegen_types/src/symbols.rs
  • baml_language/crates/baml_project/src/client_codegen.rs
  • baml_language/sdk_tests/crates/rust/function_calls/customizable/test_callback_throws.rs
  • baml_language/sdk_tests/crates/rust/function_calls/customizable/test_generic_calls.rs
  • baml_language/sdk_tests/crates/rust/function_calls/customizable/test_generic_inference.rs
  • baml_language/sdk_tests/crates/rust/function_calls/customizable/test_host_callables.rs
  • baml_language/sdk_tests/crates/rust/function_calls/customizable/test_raises.rs
  • baml_language/sdk_tests/crates/rust/type_shapes/customizable/roundtrip_tests/test_forward_refs.rs
  • baml_language/sdk_tests/crates/rust/type_shapes/customizable/roundtrip_tests/test_generics.rs
  • baml_language/sdk_tests/crates/rust/type_shapes/customizable/roundtrip_tests/test_primitives.rs
  • baml_language/sdk_tests/crates/rust/type_shapes/customizable/test_generic.rs
  • baml_language/sdk_tests/fixtures/function_calls/baml_src/ns_host_callable_tests/main.baml
  • baml_language/sdk_tests/harness_setup/src/rust.rs
  • baml_language/sdks/rust/bridge_rust/src/baml_value.rs
  • baml_language/sdks/rust/bridge_rust/src/capi.rs
  • baml_language/sdks/rust/bridge_rust/src/encode.rs
  • baml_language/sdks/rust/bridge_rust/src/error.rs
  • baml_language/sdks/rust/bridge_rust/src/host_value.rs
  • baml_language/sdks/rust/bridge_rust/src/lib.rs
  • baml_language/sdks/rust/bridge_rust/src/runtime.rs
  • baml_language/sdks/rust/bridge_rust/tests/conversions.rs
  • baml_language/sdks/rust/bridge_rust/tests/live_engine.rs
  • baml_language/sdks/rust/sdkgen_rust/src/analyze.rs
  • baml_language/sdks/rust/sdkgen_rust/src/effect_rename.rs
  • baml_language/sdks/rust/sdkgen_rust/src/emit/class.rs
  • baml_language/sdks/rust/sdkgen_rust/src/emit/enum_.rs
  • baml_language/sdks/rust/sdkgen_rust/src/emit/function.rs
  • baml_language/sdks/rust/sdkgen_rust/src/emit/union.rs
  • baml_language/sdks/rust/sdkgen_rust/src/lib.rs
  • baml_language/sdks/rust/sdkgen_rust/src/translate_ty.rs
  • baml_language/sdks/rust/sdkgen_rust/src/unions.rs

@vercel
vercel Bot temporarily deployed to Preview – beps July 21, 2026 17:48 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 21, 2026 17:55 Inactive
Also enable/update the tests
The rust signature should reflect the implicit generic parameter for a
callable-typed function parameter that omits the `throws` clause. This
had been excluded due to the limitations of python and typescript, but
rust requires the information be provided.
Also adds some tests.
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 21, 2026 18:09 Inactive
@vercel
vercel Bot temporarily deployed to Preview – beps July 21, 2026 18:17 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 21, 2026 18:23 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 21, 2026 18:44 Inactive
@2kai2kai2
2kai2kai2 enabled auto-merge July 21, 2026 18:45
@vercel
vercel Bot temporarily deployed to Preview – beps July 21, 2026 18:45 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle2 July 21, 2026 18:52 Inactive
@vercel
vercel Bot temporarily deployed to Preview – promptfiddle July 21, 2026 19:06 Inactive
@2kai2kai2
2kai2kai2 added this pull request to the merge queue Jul 21, 2026
Merged via the queue into canary with commit 82e3105 Jul 21, 2026
66 checks passed
@2kai2kai2
2kai2kai2 deleted the kai/rust-bridge branch July 21, 2026 19:22

This branch was previously deployed

3 inactive deployments
Preview – promptfiddle — eedc99f3 Deployed Jul 21, 2026 by vercel[bot]
Preview – promptfiddle2 — eedc99f3 Deployed Jul 21, 2026 by vercel[bot]
Preview – beps — eedc99f3 Deployed Jul 21, 2026 by vercel[bot]
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