Skip to content

Conversation

CeciliaZ030
Copy link

@CeciliaZ030 CeciliaZ030 commented Aug 20, 2025

Summary

Adds first-pass native Rust code generation:

  • Converts BAML IR directly into idiomatic Rust types.
  • Integrates Askama templates for Rust emission.
  • Scaffolds function stubs and generator plumbing.

Repo: aomi-labs/baml

What's changed

  • Rust generator updates under engine/generators/languages/rust/src/** to emit native Rust types from IR.
  • IR→Rust pipelines for classes, enums, unions, and function stubs.
  • Integration hooks in generator library and CLI paths to enable Rust codegen.

Testing

  • Ran generator locally; emitted Rust builds for representative IR structures.
  • Smoke-tested function stubs and type shapes.

Next PR

  • Implement rust_language_client.
  • Decide process model to avoid FFI:
    • Spawn client as a subprocess of the server, or
    • Spawn server as a subprocess of the client.

Checklist

  • IR → native Rust type emission
  • Askama template integration
  • Integration tests + CI + docs
  • rust_language_client (next PR)
  • Subprocess orchestration + protocol (next PR)

Important

Adds Rust code generation for BAML IR using Askama templates, with support for classes, enums, unions, and function stubs.

  • Behavior:
    • Converts BAML IR to Rust types using Askama templates in engine/generators/languages/rust/src/**.
    • Adds generate_sdk_files in lib.rs to generate Rust SDK files.
    • Supports classes, enums, unions, and function stubs.
  • Templates:
    • Defines Cargo.toml and lib.rs templates for Rust client.
  • Integration:
    • Prepares generate_sdk in generators_lib/src/lib.rs for Rust codegen (marked as todo).
  • CFFI:
    • Updates cffi.pb.go for CFFI protocol buffer changes.

This description was created by Ellipsis for 2309b48. You can customize this summary. It will automatically update as commits are pushed.

CeciliaZ030 and others added 3 commits August 19, 2025 22:09
- Add BAML-RUST.md: Comprehensive analysis of Rust client implementation approach
- Add BAML-GO.md: Deep dive into Go client FFI architecture and performance
- Document why OpenAPI-generated Rust clients are limited (~60% feature loss)
- Outline implementation plan for native Rust client generator
- Compare direct FFI clients vs HTTP-based clients

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Copy link

🚀 Code Review Initiated

The review process for this pull request has started. Our system is analyzing the changes for:

  • Code quality, performance, and potential issues
  • Adherence to project guidelines
  • Integration of user-provided learnings

You will receive structured and actionable feedback shortly! ⏳

Copy link

vercel bot commented Aug 20, 2025

@CeciliaZ030 is attempting to deploy a commit to the Boundary Team on Vercel.

A member of the Team first needs to authorize it.

@CeciliaZ030 CeciliaZ030 changed the title Native Rust codegen: BAML IR to Rust types with Client Templates [WIP] Native Rust codegen: BAML IR to Rust types with Client Templates Aug 20, 2025
Copy link
Contributor

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

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

Caution

Changes requested ❌

Reviewed everything up to 2309b48 in 6 minutes and 9 seconds. Click for details.
  • Reviewed 7760 lines of code in 30 files
  • Skipped 0 files when reviewing.
  • Skipped posting 3 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
1. engine/baml-lib/baml-types/src/generator.rs:35
  • Draft comment:
    The new Rust variant is added with default client mode set to Sync. Please double-check whether Sync is the desired default for Rust (vs. async) and document the rationale.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 0% <= threshold 50% The comment asks the PR author to double-check their choice and document the rationale, which violates the rule against asking for confirmation or explanation. It does not provide a specific suggestion or point out a clear issue with the code.
2. engine/generators/languages/rust/src/type.rs:305
  • Draft comment:
    There is a duplicate implementation of safe_rust_identifier. Consolidate these into a single utility function to avoid maintenance issues.
  • Reason this comment was not posted:
    Comment looked like it was already resolved.
3. engine/language_client_go/pkg/cffi/cffi.pb.go:328
  • Draft comment:
    The generated protobuf code now omits the combined check for protoimpl.UnsafeEnabled && x != nil and always checks for x != nil. Ensure this change is intentional and document the rationale if it’s due to an update in protoc-gen-go.
  • Reason this comment was not posted:
    Comment did not seem useful. Confidence is useful = 30% <= threshold 50% The comment is asking the author to ensure that a change is intentional and to document the rationale. This violates the rule against asking the author to confirm their intention or to update documentation. However, it does point out a specific change in behavior, which could be useful if it was framed differently.

Workflow ID: wflow_nAH9TterNQA9PnWn

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

}}

"#,
func.name.to_lowercase(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Using to_lowercase() on the function name may be insufficient if the original name isn’t already in snake_case. Consider applying a proper snake_case conversion (e.g. via the provided to_snake_case in utils) to ensure valid Rust identifiers.

path.push_str("crate::");
} else if i > 0 {
path.push_str(part);
path.push_str("::");
Copy link
Contributor

Choose a reason for hiding this comment

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

The module path built in relative_from appends '::' after each part. Consider trimming the trailing separator for a cleaner module path when concatenating with type names.

cFunctionName := C.CString(functionName)
defer C.free(unsafe.Pointer(cFunctionName))

result := C.WrapCallFunctionStreamFromC(runtime, cFunctionName, cEncodedArgs, C.uintptr_t(len(encodedArgs)), C.uint32_t(id))
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo: The variable 'cEncodedArgs' is used here, but the function parameter is named 'encodedArgs'. This appears to be a lexicographical error and should be corrected.

Suggested change
result := C.WrapCallFunctionStreamFromC(runtime, cFunctionName, cEncodedArgs, C.uintptr_t(len(encodedArgs)), C.uint32_t(id))
result := C.WrapCallFunctionStreamFromC(runtime, cFunctionName, encodedArgs, C.uintptr_t(len(encodedArgs)), C.uint32_t(id))

@@ -113,6 +113,9 @@ pub fn check_version(
GeneratorOutputType::Go => {
format!("go install github.com/boundaryml/baml/go@{gen_version}")
}
GeneratorOutputType::Rust => {
format!("cargo install baml-py --version {gen_version}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo note: The Rust branch command uses "cargo install baml-py --version {gen_version}". Is "baml-py" the intended package name for Rust, or should it be something like "baml-rs" to match the Rust target? Please verify.

Suggested change
format!("cargo install baml-py --version {gen_version}")
format!("cargo install baml-rs --version {gen_version}")

CeciliaZ030 and others added 7 commits August 21, 2025 21:05
- Refactor Rust client generation templates for improved code structure
- Remove redundant runtime.rs.j2 template
- Update generator configuration to support Rust client output
- Add Rust generator to BAML test configuration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
➜  generators: ✗ cargo test --package generators-rust
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