Skip to content

Commit 2b21d76

Browse files
committed
Add __baml_class__ and __baml_enum__ keys into parsed maps
1 parent e4fc5da commit 2b21d76

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

README.md

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ Uses the BAML Rust NIF to call the BAML library.
66
What this library does:
77

88
- Call functions in BAML files.
9-
- Make use of the BAML LLM client to call LLM functions.
9+
- Switch between different LLM clients.
10+
- Get usage data using collectors.
1011

1112
What this library does not do:
1213

13-
- Generate `baml_client` Elixir client code from BAML files.
14+
- Code generation of Elixir `baml_client` from BAML files.
15+
- Automatically parse BAML results into Elixir structs.
1416

1517
## Usage
1618

@@ -54,7 +56,6 @@ function ExtractResume(resume: string) -> Resume {
5456
Now call the BAML function:
5557

5658
```elixir
57-
# from: The path to the baml_src directory.
5859
alias BamlElixir.Client
5960

6061
Client.new()
@@ -71,6 +72,35 @@ Client.new()
7172
|> Enum.each(&IO.inspect/1)
7273
```
7374

75+
#### Parsing results
76+
77+
If BAML returns a class type, you will get a map with keys as atoms and a special key `__baml_class__` with the BAML class name.
78+
79+
Example:
80+
81+
```elixir
82+
%{
83+
__baml_class__: "Resume",
84+
name: "John Doe",
85+
job_title: "CTO",
86+
company: %{
87+
__baml_class__: "Company",
88+
name: "Acme Inc."
89+
}
90+
}
91+
```
92+
93+
If BAML returns an enum type, you will get a map two special keys: `__baml_enum__` with the BAML enum name and `value` with the enum value.
94+
95+
Example:
96+
97+
```elixir
98+
%{
99+
__baml_enum__: "Color",
100+
value: "Red"
101+
}
102+
```
103+
74104
### Images
75105

76106
Send an image URL:
@@ -137,7 +167,7 @@ Add baml_elixir to your mix.exs:
137167
```elixir
138168
def deps do
139169
[
140-
{:baml_elixir, "~> 1.0.0-pre.3"}
170+
{:baml_elixir, "~> 1.0.0-pre.4"}
141171
]
142172
end
143173
```

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule BamlElixir.MixProject do
22
use Mix.Project
33

4-
@version "1.0.0-pre.3"
4+
@version "1.0.0-pre.4"
55

66
def project do
77
[

native/baml_elixir/src/lib.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn baml_value_to_term<'a>(env: Env<'a>, value: &BamlValue, client: &Client) -> N
8282
.collect();
8383
Ok(terms?.encode(env))
8484
}
85-
BamlValue::Map(map) | BamlValue::Class(_, map) => {
85+
BamlValue::Map(map) => {
8686
let mut result_map = Term::map_new(env);
8787
for (key, value) in map.iter() {
8888
let value_term = baml_value_to_term(env, value, client)?;
@@ -92,18 +92,39 @@ fn baml_value_to_term<'a>(env: Env<'a>, value: &BamlValue, client: &Client) -> N
9292
}
9393
Ok(result_map)
9494
}
95+
BamlValue::Class(class_name, map) => {
96+
let mut result_map = Term::map_new(env);
97+
let class_atom = rustler::Atom::from_str(env, "__baml_class__")
98+
.map_err(|_| Error::Term(Box::new("Failed to create atom")))?;
99+
result_map = result_map
100+
.map_put(class_atom.encode(env), class_name.encode(env))
101+
.map_err(|_| Error::Term(Box::new("Failed to add class name")))?;
102+
for (key, value) in map.iter() {
103+
let key_atom = rustler::Atom::from_str(env, key)
104+
.map_err(|_| Error::Term(Box::new("Failed to create key atom")))?;
105+
let value_term = baml_value_to_term(env, value, client)?;
106+
result_map = result_map
107+
.map_put(key_atom.encode(env), value_term)
108+
.map_err(|_| Error::Term(Box::new("Failed to add key to map")))?;
109+
}
110+
Ok(result_map)
111+
}
95112
BamlValue::Media(_media) => {
96113
// For now, return an error since we need to check the actual BamlMedia structure
97114
Err(Error::Term(Box::new("Media type not yet supported")))
98115
}
99116
BamlValue::Enum(enum_type, variant) => {
100-
// Convert enum to a map with type and variant
117+
// Convert enum to a map with __baml_enum__ and value
101118
let mut result_map = Term::map_new(env);
119+
let enum_atom = rustler::Atom::from_str(env, "__baml_enum__")
120+
.map_err(|_| Error::Term(Box::new("Failed to create enum atom")))?;
121+
let value_atom = rustler::Atom::from_str(env, "value")
122+
.map_err(|_| Error::Term(Box::new("Failed to create value atom")))?;
102123
result_map = result_map
103-
.map_put("type".encode(env), enum_type.encode(env))
124+
.map_put(enum_atom.encode(env), enum_type.encode(env))
104125
.map_err(|_| Error::Term(Box::new("Failed to add enum type")))?;
105126
result_map = result_map
106-
.map_put("variant".encode(env), variant.encode(env))
127+
.map_put(value_atom.encode(env), variant.encode(env))
107128
.map_err(|_| Error::Term(Box::new("Failed to add enum variant")))?;
108129
Ok(result_map)
109130
}

0 commit comments

Comments
 (0)