Skip to content

Commit d90e7a3

Browse files
aaronvgcursoragent
andauthored
Document baml tag setting and retrieval (#2534)
# Pull Request Template Thanks for taking the time to fill out this pull request! ## Issue Reference Please link to any related issues - [ ] This PR fixes/closes #[issue number] ## Changes Please describe the changes proposed in this pull request This PR adds comprehensive documentation for BAML's tagging feature across Python, TypeScript, and Go. It clarifies how to set tags at both the parent trace and per-call level, and how to retrieve them using the `Collector`. This enhances observability by providing clear guidance on attaching and accessing custom metadata for BAML function calls. Specifically: - A new "Tags" section in the Observability guide explains tag usage and retrieval. - The Collector reference now includes a dedicated "Tags" subsection with examples. - The `with_options` API reference details the `tags` parameter for per-call configuration. ## Testing Please describe how you tested these changes - [ ] Unit tests added/updated - [X] Manual testing performed (verified documentation content and code examples) - [ ] Tested in [environment] ## Screenshots If applicable, add screenshots to help explain your changes [Add screenshots here...] ## PR Checklist Please ensure you've completed these items - [X] I have read and followed the contributing guidelines - [X] My code follows the style guidelines of this project - [X] I have performed a self-review of my own code - [X] I have commented my code, particularly in hard-to-understand areas (N/A for markdown) - [X] I have made corresponding changes to the documentation - [X] My changes generate no new warnings ## Additional Notes Add any other context about the PR here The documentation includes examples for Python, TypeScript, and Go, and clarifies how tags from parent traces are inherited and merged with per-call tags. --- <a href="https://cursor.com/background-agent?bcId=bc-e24c1fe2-93be-43f3-9f25-e5e15ca7a22b"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/open-in-cursor-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/open-in-cursor-light.svg"><img alt="Open in Cursor" src="https://cursor.com/open-in-cursor.svg"></picture></a>&nbsp;<a href="https://cursor.com/agents?id=bc-e24c1fe2-93be-43f3-9f25-e5e15ca7a22b"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/open-in-web-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/open-in-web-light.svg"><img alt="Open in Web" src="https://cursor.com/open-in-web.svg"></picture></a> <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Adds concise docs for setting/inheriting per-call tags and retrieving them via Collector across Python/TS/Go, and removes the id-based lookup/docs, updating examples accordingly. > > - **Guide**: > - Adds "Tags on BAML calls and retrieving them with the Collector" with Python/TypeScript/Go examples showing parent-trace tag inheritance, per-call tags (`baml_options`/options/`WithTags`), and reading `collector.last.tags`/`log.Tags()`. > - **Reference: Collector**: > - New "Tags" subsection with examples for setting and reading merged tags. > - API note: remove `collector.id(id)` from methods; recommend correlating via `tags`. > - **Reference: with_options**: > - Adds "Per-call Tags" examples (Python/TS/Go) and mentions `tags` param; usage examples updated to use `collector.last`/`logs` and tags for correlation in parallel/streaming. > - **Removals**: > - Deletes `fern/03-reference/baml_client/id.mdx` (id feature doc). > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 22b3e58. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
1 parent aba814b commit d90e7a3

4 files changed

Lines changed: 244 additions & 222 deletions

File tree

‎fern/01-guide/07-observability/studio.mdx‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,113 @@ async def pre_process_text(text):
178178
set_tags(**tags_dict) # "**" unpacks the dictionary
179179
return text.replace("\n", " ")
180180
```
181+
182+
### Tags on BAML calls and retrieving them with the Collector
183+
184+
You can also set tags directly on a BAML function call and then retrieve them from the `Collector`. Tags from a parent trace are inherited by the BAML function call and merged with any function-specific tags you pass.
185+
186+
<Tabs>
187+
<Tab title="Python" language="python">
188+
```python
189+
from baml_client import b
190+
from baml_client.tracing import trace, set_tags
191+
from baml_py import Collector
192+
193+
@trace
194+
async def parent_fn(msg: str):
195+
# Set tags on the parent trace (these propagate to child BAML calls)
196+
set_tags(parent_id="p123", run="xyz")
197+
198+
collector = Collector(name="tags-collector")
199+
200+
# You can also set per-call tags via baml_options
201+
await b.TestOpenAIGPT4oMini(
202+
msg,
203+
baml_options={
204+
"collector": collector,
205+
"tags": {"call_id": "first", "version": "v1"},
206+
},
207+
)
208+
209+
# Retrieve tags from the last function log
210+
log = collector.last
211+
assert log is not None
212+
print(log.tags) # {"parent_id": "p123", "run": "xyz", "call_id": "first", "version": "v1"}
213+
```
214+
</Tab>
215+
216+
<Tab title="TypeScript" language="typescript">
217+
```typescript
218+
import { b } from "baml_client";
219+
import { Collector } from "@boundaryml/baml";
220+
import { traceAsync, setTags } from "../baml_client/tracing";
221+
222+
const parent = traceAsync("parentTS", async (msg: string) => {
223+
setTags({ parentId: "p123", run: "xyz" });
224+
225+
const collector = new Collector("tags-collector");
226+
227+
await b.TestOpenAIGPT4oMini(msg, {
228+
collector,
229+
tags: { callId: "first", version: "v1" },
230+
});
231+
232+
const log = collector.last!;
233+
const tags = log.tags;
234+
console.log(tags); // { parentId: "p123", run: "xyz", callId: "first", version: "v1" }
235+
});
236+
237+
await parent("hi");
238+
```
239+
</Tab>
240+
241+
<Tab title="Go" language="go">
242+
```go
243+
package main
244+
245+
import (
246+
"context"
247+
"fmt"
248+
b "example.com/integ-tests/baml_client"
249+
)
250+
251+
func run() error {
252+
ctx := context.Background()
253+
254+
collector, err := b.NewCollector("tags-collector")
255+
if err != nil {
256+
return err
257+
}
258+
259+
// Set per-call tags using WithTags
260+
tags := map[string]string{
261+
"callId": "first",
262+
"version": "v1",
263+
}
264+
_, err = b.TestOpenAIGPT4oMini(ctx, "hello", b.WithCollector(collector), b.WithTags(tags))
265+
if err != nil {
266+
return err
267+
}
268+
269+
logs, err := collector.Logs()
270+
if err != nil {
271+
return err
272+
}
273+
if len(logs) > 0 {
274+
t, err := logs[0].Tags()
275+
if err != nil {
276+
return err
277+
}
278+
fmt.Printf("Tags: %+v\n", t)
279+
}
280+
return nil
281+
}
282+
```
283+
</Tab>
284+
</Tabs>
285+
286+
Notes:
287+
- Tags from `set_tags`/`setTags` on a parent `trace` are merged into the BAML function’s tags.
288+
- Per-call tags are provided via `baml_options` in Python and the options object in TypeScript; in Go use `b.WithTags(map[string]string)`.
289+
- Retrieve tags from a `FunctionLog` using `log.tags` (Python/TypeScript) or `log.Tags()` (Go).
290+

‎fern/03-reference/baml_client/collector.mdx‎

Lines changed: 71 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)