Skip to main content

Sending OpenAI Chat Completion Traces

This guide explains how to send OpenTelemetry-compatible traces for OpenAI chat completions to Langtrace using cURL. Here’s an example of how your traces will appear in the Langtrace UI after following this guide: OpenAI Chat Completion Trace Example

Prerequisites

  • Langtrace API Key
  • OpenAI API Key (for comparison with SDK usage)

Endpoint

Headers

Trace Format

The trace must follow the OpenTelemetry format and include specific attributes for OpenAI chat completions. Note that attributes must use the OpenTelemetry value format with stringValue or intValue:

Example cURL Command

Required Attributes

Function Calling Example

When using OpenAI function calling, include the tools in the attributes with proper value formatting:

Response Format

Success Response

Error Response

Comparison with SDK Usage

For comparison, here’s how the same trace would be generated using the Python SDK:

Best Practices

  1. Always include required attributes (vendor, model, messages, path)
  2. Generate unique trace and span IDs for each request
  3. Use accurate timestamps for start and end times
  4. Include usage statistics when available
  5. Format message arrays and tool calls as proper JSON strings
  6. Use the OpenTelemetry user agent to ensure proper trace processing
  7. Include resource attributes with service information
  8. Use proper OpenTelemetry value format (stringValue/intValue) for attributes