feat(attachments): add json attachment uploads#125
Open
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit intomainfrom
Open
feat(attachments): add json attachment uploads#125Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit intomainfrom
Abhijeet Prasad (AbhiPrasad) wants to merge 1 commit intomainfrom
Conversation
Add a public JSON attachment API that serializes JSON, stores an attachment
reference on an OpenTelemetry span, and uploads the JSON payload in the
background. The upload queue is owned by the Braintrust client and is flushed or
shut down with the configured tracer provider/span processor.
New public API:
- (*braintrust.Client).SetJSONAttachment(ctx, span, key, data, opts...)
serializes data as JSON, sets key on the span to a Braintrust attachment
reference, enqueues the upload, and returns the AttachmentReference.
- trace/attachment.JSON is the JSON MIME type constant.
- trace/attachment.WithFilename(name) sets the displayed attachment filename.
- trace/attachment.WithPrettyJSON() writes indented JSON.
- trace/attachment.AttachmentReference is the public reference shape stored on
the span.
Initialize Braintrust with a tracer provider and the normal client options:
```go
tp := sdktrace.NewTracerProvider()
defer tp.Shutdown(context.Background())
bt, err := braintrust.New(tp,
braintrust.WithAPIKey(os.Getenv("BRAINTRUST_API_KEY")),
braintrust.WithProject("my-project"),
)
if err != nil {
log.Fatal(err)
}
tracer := bt.Tracer("my-app")
```
Send a JSON attachment by starting a span and passing that span to the client:
```go
ctx, span := tracer.Start(context.Background(), "example")
defer span.End()
_, err = bt.SetJSONAttachment(ctx, span, "braintrust.input_json", map[string]any{
"messages": []map[string]string{
{"role": "user", "content": "Summarize this conversation."},
},
},
attachment.WithFilename("conversation.json"),
attachment.WithPrettyJSON(),
)
if err != nil {
log.Fatal(err)
}
```
Use json.RawMessage when the JSON is already encoded. Passing a raw []byte to
SetJSONAttachment follows encoding/json behavior and produces a base64 JSON
string instead of embedding the bytes as JSON.
71a488a to
737bf07
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a public JSON attachment API that serializes JSON, stores an attachment reference on an OpenTelemetry span, and uploads the JSON payload in the background. The upload queue is owned by the Braintrust client and is flushed or shut down with the configured tracer provider/span processor.
New public API:
Initialize Braintrust with a tracer provider and the normal client options:
Send a JSON attachment by starting a span and passing that span to the client: