Documentation
¶
Overview ¶
Package braintrust provides the core Braintrust SDK for Go.
Braintrust is a platform for building reliable AI applications. This SDK provides tools for evaluation, experimentation, and observability of AI systems.
Main Packages ¶
For AI model evaluations, see the eval package.
For distributed tracing and observability, see the trace package.
Configuration ¶
The SDK reads configuration from environment variables. See [GetConfig] for a complete list of supported environment variables.
Learn More ¶
- Examples: https://github.com/braintrustdata/braintrust-sdk-go/tree/main/examples
- Documentation: https://www.braintrust.dev/docs
Index ¶
- func NewEval[I, R any](client *Client, e *eval.Eval[I, R]) *eval.Eval[I, R]
- func NewEvaluator[I, R any](client *Client) *eval.Evaluator[I, R]
- func NewWorkflowEval[I, R any](client *Client, definition *eval.WorkflowEval[I, R]) *eval.WorkflowEval[I, R]
- type Client
- func (c *Client) API() *api.APIdeprecated
- func (c *Client) LoadPrompt(ctx context.Context, opts prompt.LoadOpts) (*prompt.Prompt, error)
- func (c *Client) Permalink(span oteltrace.Span) string
- func (c *Client) String() string
- func (c *Client) Tracer(name string, opts ...oteltrace.TracerOption) oteltrace.Tracer
- func (c *Client) TracerProvider() *trace.TracerProvider
- type Option
- func EnableBuiltinAdkTraces() Option
- func WithAPIKey(apiKey string) Option
- func WithAPIURL(apiURL string) Option
- func WithAppURL(appURL string) Option
- func WithAutoConvertAIAttachments(enabled bool) Option
- func WithBlockingLogin(enabled bool) Option
- func WithEnableTraceConsoleLog(enabled bool) Option
- func WithEnvironment(environmentType string, name ...string) Option
- func WithExporter(exporter trace.SpanExporter) Option
- func WithFilterAISpans(enabled bool) Option
- func WithLogger(l logger.Logger) Option
- func WithOrgName(orgName string) Option
- func WithProject(projectName string) Option
- func WithProjectID(projectID string) Option
- func WithSpanCustomizers(customizers ...config.SpanCustomizer) Option
- func WithSpanFilterFuncs(filterFuncs ...config.SpanFilterFunc) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewEval ¶ added in v0.12.0
NewEval creates a runnable eval.Eval by combining a client with an eval definition.
Example:
client, _ := braintrust.New(tp)
e := braintrust.NewEval(client, &eval.Eval[string, string]{
Name: "classify",
Task: task,
Scorers: scorers,
})
result, _ := e.Run(ctx, eval.RunOpts[string, string]{Dataset: dataset})
func NewEvaluator ¶
NewEvaluator creates a new evaluator for running evaluations with the same input and output types.
func NewWorkflowEval ¶ added in v0.18.0
func NewWorkflowEval[I, R any](client *Client, definition *eval.WorkflowEval[I, R]) *eval.WorkflowEval[I, R]
NewWorkflowEval attaches the client to a durable workflow evaluation.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main Braintrust SDK client
func New ¶
func New(tp *trace.TracerProvider, opts ...Option) (*Client, error)
New creates a new Braintrust client.
It will add a Braintrust exporter to the given tracer provider..
Configuration is loaded from environment variables first, then explicit options are applied (options take precedence).
Login happens asynchronously in the background by default.
Example:
tp := trace.NewTracerProvider()
bt, err := braintrust.New(tp,
braintrust.WithAPIKey("your-api-key"),
braintrust.WithProject("my-project"),
)
if err != nil {
log.Fatal(err)
}
defer tp.Shutdown(context.Background())
func (*Client) API
deprecated
API returns the legacy API facade for source compatibility. SDK evaluation and prompt loading use the generated openapi.Client instead. For new direct REST integrations, use the openapi package.
Deprecated: Use openapi.NewClientWithResponses for direct REST operations. Continue to use Client.LoadPrompt, NewEvaluator, and NewWorkflowEval for SDK-managed prompt loading and evaluations.
func (*Client) LoadPrompt ¶ added in v0.13.0
LoadPrompt loads a prompt saved in Braintrust and returns it ready to render.
p, err := client.LoadPrompt(ctx, prompt.LoadOpts{Slug: "summarizer"})
if err != nil {
return err
}
built, err := p.Build(map[string]any{"input": article})
Opts.Project defaults to the client's project. The prompt is fetched every time; nothing is cached.
func (*Client) Permalink ¶
Permalink returns a URL to the span in the Braintrust UI. If the permalink cannot be generated, it returns an empty string and logs a warning.
Example:
client, _ := braintrust.New(tp, braintrust.WithAPIKey("your-key"))
tracer := client.Tracer("my-app")
ctx, span := tracer.Start(ctx, "my-operation")
defer span.End()
// Get the permalink
link := client.Permalink(span)
fmt.Println("View trace:", link)
func (*Client) Tracer ¶
Tracer returns an OpenTelemetry Tracer with the given name. This is a convenience method equivalent to calling TracerProvider().Tracer(name, opts...).
Example:
tracer := client.Tracer("my-app")
ctx, span := tracer.Start(ctx, "my-operation")
defer span.End()
func (*Client) TracerProvider ¶
func (c *Client) TracerProvider() *trace.TracerProvider
TracerProvider returns the OpenTelemetry TracerProvider used by this client. This can be used to create tracers or access the provider for advanced use cases.
type Option ¶
Option is a functional option for configuring a Braintrust client
func EnableBuiltinAdkTraces ¶ added in v0.2.1
func EnableBuiltinAdkTraces() Option
EnableBuiltinAdkTraces can be used to enable exporting spans from Google ADK's built-in telemetry (gcp.vertex.agent), which are not exported to Braintrust by default.
func WithAPIKey ¶
WithAPIKey sets a nonblank API key (overrides BRAINTRUST_API_KEY). Blank or whitespace-only values are treated as unset and leave environment and .braintrust.json fallback lookup available.
func WithAPIURL ¶
WithAPIURL sets the API URL (overrides BRAINTRUST_API_URL)
func WithAppURL ¶
WithAppURL sets the app URL (overrides BRAINTRUST_APP_URL)
func WithAutoConvertAIAttachments ¶ added in v0.7.2
WithAutoConvertAIAttachments controls whether the SDK automatically scans spans for base64 LLM attachments and replaces them with uploaded references. Defaults to true. Environment variable: BRAINTRUST_AUTO_CONVERT_AI_ATTACHMENTS
func WithBlockingLogin ¶
WithBlockingLogin blocks setup until login completes. Authentication normally happens asynchronously in the background.
This only enables printing links to spans. This is only useful for scripts, testing and examples. It is not recommended for production use.
func WithEnableTraceConsoleLog ¶ added in v0.5.0
WithEnableTraceConsoleLog enables logging traces to stdout for debugging. Environment variable: BRAINTRUST_ENABLE_TRACE_CONSOLE_LOG
func WithEnvironment ¶ added in v0.10.0
WithEnvironment sets span-origin environment provenance.
func WithExporter ¶
func WithExporter(exporter trace.SpanExporter) Option
WithExporter injects a custom OpenTelemetry SpanExporter. If not provided, an OTLP HTTP exporter will be created automatically. This is solely for testing purposes.
func WithFilterAISpans ¶
WithFilterAISpans enables filtering to keep only AI-related spans When enabled, only spans with AI-related names or attributes will be sent
func WithLogger ¶
WithLogger sets a custom logger for the SDK If not provided, a default logger will be used
func WithOrgName ¶
WithOrgName sets the organization name (overrides BRAINTRUST_ORG_NAME)
func WithProject ¶
WithProject sets the default project name (overrides BRAINTRUST_DEFAULT_PROJECT)
func WithProjectID ¶
WithProjectID sets the default project ID (overrides BRAINTRUST_DEFAULT_PROJECT_ID)
func WithSpanCustomizers ¶ added in v0.16.0
func WithSpanCustomizers(customizers ...config.SpanCustomizer) Option
WithSpanCustomizers appends export customizers in execution order. The option snapshots the supplied list; constructing the exporter snapshots it again. Callback closure state remains the caller's responsibility.
func WithSpanFilterFuncs ¶
func WithSpanFilterFuncs(filterFuncs ...config.SpanFilterFunc) Option
WithSpanFilterFuncs adds custom span filter functions Filters are evaluated in order. Return >0 to keep, <0 to drop, 0 to continue
Directories
¶
| Path | Synopsis |
|---|---|
|
Package api provides the Braintrust REST facade retained for compatibility.
|
Package api provides the Braintrust REST facade retained for compatibility. |
|
datasets
Package datasets provides operations for managing Braintrust datasets.
|
Package datasets provides operations for managing Braintrust datasets. |
|
experiments
Package experiments provides operations for managing Braintrust experiments.
|
Package experiments provides operations for managing Braintrust experiments. |
|
functions
Package functions provides operations for managing Braintrust functions (prompts, tools, scorers).
|
Package functions provides operations for managing Braintrust functions (prompts, tools, scorers). |
|
projects
Package projects provides operations for managing Braintrust projects.
|
Package projects provides operations for managing Braintrust projects. |
|
prompts
Package prompts provides read access to the prompts stored in Braintrust.
|
Package prompts provides read access to the prompts stored in Braintrust. |
|
btx
module
|
|
|
Package config provides configuration management for the Braintrust SDK.
|
Package config provides configuration management for the Braintrust SDK. |
|
Package eval provides tools for evaluating AI model outputs.
|
Package eval provides tools for evaluating AI model outputs. |
|
Package evalrunner runs Braintrust evals under the `bt` CLI.
|
Package evalrunner runs Braintrust evals under the `bt` CLI. |
|
examples
module
|
|
|
internal
|
|
|
apikey
Package apikey provides Braintrust API key discovery helpers.
|
Package apikey provides Braintrust API key discovery helpers. |
|
auth
Package auth provides authentication functionality for the Braintrust SDK.
|
Package auth provides authentication functionality for the Braintrust SDK. |
|
evalhooks
Package evalhooks carries the callbacks the eval runner needs to observe a run while it is still in flight.
|
Package evalhooks carries the callbacks the eval runner needs to observe a run while it is still in flight. |
|
genapi
command
Command genapi fetches the pinned OpenAPI specification and generates REST bindings.
|
Command genapi fetches the pinned OpenAPI specification and generates REST bindings. |
|
genorchestrion
Package genorchestrion generates combined orchestrion.yml files from individual provider configs.
|
Package genorchestrion generates combined orchestrion.yml files from individual provider configs. |
|
genorchestrion/cmd
command
Command genorchestrion generates the combined orchestrion.yml file.
|
Command genorchestrion generates the combined orchestrion.yml file. |
|
https
Package https provides a unified HTTP client for making API requests with centralized auth, error handling, and debug logging.
|
Package https provides a unified HTTP client for making API requests with centralized auth, error handling, and debug logging. |
|
logger
Package logger provides internal logging utilities.
|
Package logger provides internal logging utilities. |
|
mustache
Package mustache is a vendored copy of github.com/cbroglie/mustache.
|
Package mustache is a vendored copy of github.com/cbroglie/mustache. |
|
nestedmodules
Package nestedmodules provides helpers for working with nested Go modules in this repository.
|
Package nestedmodules provides helpers for working with nested Go modules in this repository. |
|
nestedmodules/cmd
command
Command nestedmodules prints nested modules in dependency order.
|
Command nestedmodules prints nested modules in dependency order. |
|
oteltest
Package oteltest provides testing utilities for OpenTelemetry tracing.
|
Package oteltest provides testing utilities for OpenTelemetry tracing. |
|
tests
Package tests provides test utilities for creating test sessions and other test helpers.
|
Package tests provides test utilities for creating test sessions and other test helpers. |
|
vcr
Package vcr provides utilities for recording and replaying HTTP interactions in tests using go-vcr.
|
Package vcr provides utilities for recording and replaying HTTP interactions in tests using go-vcr. |
|
Package logger provides logging interfaces and implementations for the Braintrust SDK.
|
Package logger provides logging interfaces and implementations for the Braintrust SDK. |
|
Package openapi provides primitives to interact with the openapi HTTP API.
|
Package openapi provides primitives to interact with the openapi HTTP API. |
|
Package prompt provides a local, renderable representation of a Braintrust prompt.
|
Package prompt provides a local, renderable representation of a Braintrust prompt. |
|
Package trace provides distributed tracing functionality for Braintrust experiments.
|
Package trace provides distributed tracing functionality for Braintrust experiments. |
|
attachment
Package attachment provides utilities for creating and managing attachments in Braintrust traces.
|
Package attachment provides utilities for creating and managing attachments in Braintrust traces. |
|
attachmentprocessor
Package attachmentprocessor scans span attributes for base64-encoded LLM attachments and replaces them with Braintrust attachment references after uploading the data to object storage.
|
Package attachmentprocessor scans span attributes for base64-encoded LLM attachments and replaces them with Braintrust attachment references after uploading the data to object storage. |
|
internal
Package internal provides shared middleware functionality for OpenTelemetry tracers.
|
Package internal provides shared middleware functionality for OpenTelemetry tracers. |
|
contrib/a2a
module
|
|
|
contrib/adk
module
|
|
|
contrib/all
module
|
|
|
contrib/anthropic
module
|
|
|
contrib/bedrockruntime
module
|
|
|
contrib/cloudwego/eino
module
|
|
|
contrib/genai
module
|
|
|
contrib/genkit
module
|
|
|
contrib/langchaingo
module
|
|
|
contrib/openai
module
|