diff --git a/api/libs/braintrust/src/types.rs b/api/libs/braintrust/src/types.rs index 76df0ba97..172b47e79 100644 --- a/api/libs/braintrust/src/types.rs +++ b/api/libs/braintrust/src/types.rs @@ -113,9 +113,9 @@ impl Span { self } - /// Alias for add_metadata - pub fn with_metadata(self, key: &str, value: &str) -> Self { - self.add_metadata(key, value) + /// Alias for add_metadata that converts any displayable value to a string + pub fn with_metadata(self, key: &str, value: T) -> Self { + self.add_metadata(key, &value.to_string()) } /// Get the span ID diff --git a/api/libs/braintrust/tests/integration_tests.rs b/api/libs/braintrust/tests/integration_tests.rs index 18cd13816..bc003ba87 100644 --- a/api/libs/braintrust/tests/integration_tests.rs +++ b/api/libs/braintrust/tests/integration_tests.rs @@ -96,7 +96,7 @@ async fn test_real_trace_with_spans() -> Result<()> { // Add a root span let root_span = trace.add_span("Root Operation", "function").await?; - let root_span = root_span + let mut root_span = root_span .with_input(json!({ "operation": "root", "parameters": { @@ -107,11 +107,24 @@ async fn test_real_trace_with_spans() -> Result<()> { .with_metadata("test_id", trace_id.clone()); // Log the root span + client.log_span(root_span.clone()).await?; + + sleep(Duration::from_secs(10)).await; + + root_span = root_span + .with_output(json!({ + "result": "success", + "parameters": { + "test": true, + "timestamp": chrono::Utc::now().to_rfc3339() + } + })); + client.log_span(root_span).await?; // Add an LLM span let llm_span = trace.add_span("LLM Call", "llm").await?; - let llm_span = llm_span + let mut llm_span = llm_span .with_input(json!({ "messages": [ { @@ -120,22 +133,27 @@ async fn test_real_trace_with_spans() -> Result<()> { } ] })) - .with_output(json!({ - "choices": [ - { - "message": { - "role": "assistant", - "content": "Hello! I'm responding to your integration test message." - } - } - ] - })) - .with_tokens(15, 12) .with_metadata("model", "test-model"); // Log the LLM span - client.log_span(llm_span).await?; + client.log_span(llm_span.clone()).await?; + sleep(Duration::from_secs(15)).await; + + llm_span = llm_span + .with_output(json!({ + "choices": [ + { + "message": { + "role": "assistant", + "content": "Hello! I'm responding to your integration test message." + } + } + ] + })); + + client.log_span(llm_span).await?; + // Add a tool span let tool_span = trace.add_span("Tool Execution", "tool").await?; let tool_span = tool_span @@ -164,7 +182,7 @@ async fn test_real_trace_with_spans() -> Result<()> { trace.finish().await?; // Allow some time for async processing - sleep(Duration::from_millis(200)).await; + sleep(Duration::from_secs(30)).await; Ok(()) }