Skip to content

add beginner-friendly openai example#4143

Open
bulbulmalik wants to merge 4 commits into
traceloop:mainfrom
bulbulmalik:feat/beginner-python-example
Open

add beginner-friendly openai example#4143
bulbulmalik wants to merge 4 commits into
traceloop:mainfrom
bulbulmalik:feat/beginner-python-example

Conversation

@bulbulmalik
Copy link
Copy Markdown

@bulbulmalik bulbulmalik commented May 14, 2026

  • I have added tests that cover my changes.
  • If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • (If applicable) I have updated the documentation accordingly.

Summary by CodeRabbit

  • New Features
    • Added a quickstart example script demonstrating how to trace OpenAI API calls, including chat completion requests with system and user messages.

Review Change Stack

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented May 14, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 14, 2026

📝 Walkthrough

Walkthrough

This PR adds a single new quickstart example script that demonstrates how to initialize Traceloop and execute a basic OpenAI chat completion request. The script initializes tracing at import time and provides a simple entry point for users to test integration with gpt-4o-mini.

Changes

OpenAI Quickstart Example

Layer / File(s) Summary
Quickstart script with Traceloop initialization
packages/sample-app/sample_app/quickstart_openai.py
Complete executable script that initializes Traceloop with batch tracing disabled, validates the OPENAI_API_KEY environment variable, sends a chat completion request to gpt-4o-mini, and includes exception handling for API failures.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A quickstart script hops into view,
With Traceloop tracing all the LLM's brew!
OpenAI whispers, the traces take flight,
Example code shining, both simple and bright! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'add beginner-friendly openai example' accurately describes the main change: adding a new beginner-focused quickstart script for OpenAI integration.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/sample-app/sample_app/quickstart_openai.py (1)

5-7: ⚡ Quick win

Add ConsoleSpanExporter to align with repository debugging guidelines.

This quickstart initializes tracing with disable_batch=True, but omits the debugging exporter. Wire ConsoleSpanExporter through Traceloop's exporter parameter to match repository patterns used in other sample applications.

Suggested patch
 import os
 from openai import OpenAI
 from traceloop.sdk import Traceloop
+from opentelemetry.sdk.trace.export import ConsoleSpanExporter

 # 1. Initialize Traceloop before calling any LLM services
 # We use disable_batch=True so traces are sent immediately (perfect for local testing!)
-Traceloop.init(disable_batch=True)
+Traceloop.init(disable_batch=True, exporter=ConsoleSpanExporter())
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/sample-app/sample_app/quickstart_openai.py` around lines 5 - 7,
Traceloop.init currently disables batching but doesn't attach a
ConsoleSpanExporter for local debugging; update the Traceloop.init call to pass
an exporter configured with ConsoleSpanExporter so traces are emitted to the
console (use ConsoleSpanExporter as the exporter parameter when calling
Traceloop.init) — locate the Traceloop.init invocation and add the exporter
argument wired to ConsoleSpanExporter.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/sample-app/sample_app/quickstart_openai.py`:
- Line 39: The file ends with a call to main() but is missing a trailing newline
which triggers linter W292; open the file containing the main() invocation and
add a single final newline character at the end of the file (after the main()
line) so the file terminates with a newline.

---

Nitpick comments:
In `@packages/sample-app/sample_app/quickstart_openai.py`:
- Around line 5-7: Traceloop.init currently disables batching but doesn't attach
a ConsoleSpanExporter for local debugging; update the Traceloop.init call to
pass an exporter configured with ConsoleSpanExporter so traces are emitted to
the console (use ConsoleSpanExporter as the exporter parameter when calling
Traceloop.init) — locate the Traceloop.init invocation and add the exporter
argument wired to ConsoleSpanExporter.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: c5a75825-05bc-417e-8ed9-f3305f41ad19

📥 Commits

Reviewing files that changed from the base of the PR and between 6d3e696 and cccd05f.

📒 Files selected for processing (1)
  • packages/sample-app/sample_app/quickstart_openai.py

print(f"❌ An error occurred: {e}")

if __name__ == "__main__":
main() No newline at end of file
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add trailing newline at end of file.

Ruff reports W292 here; please add a final newline to satisfy linting.

🧰 Tools
🪛 Ruff (0.15.12)

[warning] 39-39: No newline at end of file

Add trailing newline

(W292)

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/sample-app/sample_app/quickstart_openai.py` at line 39, The file
ends with a call to main() but is missing a trailing newline which triggers
linter W292; open the file containing the main() invocation and add a single
final newline character at the end of the file (after the main() line) so the
file terminates with a newline.

@bulbulmalik bulbulmalik changed the title Add my first python example add beginner-friendly openai example May 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants