Commit a74b24a
authored
Host values in BAML (#3571)
The problem: we want to be able to pass lambdas/closures/function
pointers into BAML from a host language in a type-safe way and let BAML
call it as if it were a normal BAML function. BAML->host handles already
exist in the language, allowing the host language to own opaque BAML
values. This PR implements the opposite: a way to have opaque handles to
host values in BAML.
## Lifecycle
1. A code-genned BAML function is called by the host language, passing
in an opaque/non-serializable value (such as a
callable/function/closure)
2. The bridge (on the host side) saves a reference to the value and
creates a new host value id. It ensures the host will not deallocate the
value until BAML informs it that it no longer has any references.
3. The bridge passes the newly created host value id across the ffi
boundary as an opaque handle.
4. The BAML side decodes the host value based on type. Currently this is
only host callables (BAML function-typed).
5. (for callables) When a BAML function call occurs on a host callable,
it will dispatch back across the host boundary with a new host call
operation.
6. When a host value handle is dropped in BAML (there are no references
to it and the GC runs on it), a message is sent to the bridge to notify
it that it may release the (real) value.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Pass host-language functions as callable parameters and invoke them
from BAML.
* Structured host-callable errors surfaced as a catchable
baml.errors.HostCallable with message/class/category/traceback.
* Cross-platform runtime support (Python, Node.js, Go, WASM) for
registering, dispatching and completing host callables.
* Strict host-return validation against declared types; sync-path
encoding fast-fails for host-callable args to avoid main-thread hangs.
<!-- review_stack_entry_start -->
[](https://app.coderabbit.ai/change-stack/BoundaryML/baml/pull/3571?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)
<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent a9a1cae commit a74b24a
156 files changed
Lines changed: 11516 additions & 539 deletions
File tree
- baml_language
- crates
- baml_builtins2
- baml_std/baml
- ns_errors
- ns_host
- src
- baml_cli/src/snapshots
- baml_tests
- projects/compiles/host_callable_call
- snapshots
- compiles
- __baml_std__
- host_callable_call
- test_expr_basic
- test_expr_name_concat
- test_expr_throwing_body
- test_old_and_new
- test_raw_string_name
- test_with_not_keyword
- testset_basic
- testset_dynamic
- testset_nested
- testset_vibes_nested
- testset_with_setup
- diagnostic_errors
- test_expr_name_type_error
- test_expr_with_runner
- test_expr_wrong_runner
- test_with_runner_ambiguity
- src/compiler2_tir/snapshots
- tests/bytecode_format/snapshots
- bex_engine
- src
- tests
- bex_events/src
- bex_external_types
- src
- bex_heap/src
- heap_debugger
- bex_project/src
- bex_resource_types
- src
- bex_vm_types/src
- bex_vm/src
- package_baml
- bridge_cffi/src
- ffi
- bridge_ctypes
- src
- types/baml_core/cffi/v1
- bridge_wasm
- src
- tests
- sys_llm/src
- build_request
- jinja
- sys_native
- src
- sys_ops/src
- sys_types/src
- tools_onionskin/src
- sdk_tests
- crates/python_pydantic2/host_callables/customizable
- fixtures/host_callables/baml_src
- sdks
- go/bridge_go
- cffi
- proto/baml_core/cffi/v1
- pkg
- tests
- nodejs/bridge_nodejs
- src
- tests
- typescript_src
- proto
- python
- rust/bridge_python/src
- baml_core/baml_py
- src/baml_core
- cffi/v1
- tests
- typescript2/app-vscode-webview/src
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
Lines changed: 18 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
113 | 114 | | |
114 | 115 | | |
115 | 116 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
| |||
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| 50 | + | |
| 51 | + | |
49 | 52 | | |
50 | 53 | | |
51 | 54 | | |
| |||
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 184 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
0 commit comments