Skip to content

feat: add support for OpenAI SDK v5 #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/instrumentation-openai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"@types/mocha": "^10.0.10",
"@types/node": "^24.0.15",
"mocha": "^11.7.1",
"openai": "4.38.3",
"openai": "5.12.2",
Copy link

@coderabbitai coderabbitai bot Aug 13, 2025

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Use a caret range for devDependency and verify Node.js >= 18 in CI/tooling

Pinning openai to an exact version is inconsistent with the rest of devDependencies and prevents receiving compatible patch/minor updates within v5. Also, the OpenAI SDK v5 requires Node.js >= 18. Make sure your CI/tooling uses Node >= 18, otherwise installs/tests may fail even if this package’s engines allow >=14.

Apply this diff to align the semver policy:

-    "openai": "5.12.2",
+    "openai": "^5.12.2",

If you want to make the Node requirement explicit in this package to avoid confusion for contributors running tests locally, consider bumping the engines field (outside this hunk). Example:

{
  "engines": {
    "node": ">=18"
  }
}

Run this script to verify Node versions configured across the repo:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Searching engines declarations across package.json files..."
rg -n '"engines"\s*:\s*{' -A 3 package.json packages/**/package.json || true

echo
echo "Inspecting GitHub Actions node setup (if any)..."
if [ -d .github/workflows ]; then
  rg -n "setup-node" -A 8 .github/workflows || true
  rg -n "node-version" -A 2 .github/workflows || true
else
  echo "No .github/workflows directory found."
fi

echo
echo "Checking local Node version config files..."
for f in .nvmrc .tool-versions .node-version; do
  if [ -f "$f" ]; then
    echo "== $f =="
    cat "$f"
    echo
  fi
done

Length of output: 3790


Align openai to semver policy & confirm Node.js ≥18

  • In packages/instrumentation-openai/package.json, change the pinned openai devDependency to a caret range so that patch/minor updates within v5 are picked up.
  • CI already sources Node.js version from .nvmrc (currently “20”), so the OpenAI SDK v5 requirement (Node ≥18) is satisfied.
  • (Optional) To make the Node.js requirement explicit for local development, you can add an engines field to this package’s package.json:
--- a/packages/instrumentation-openai/package.json
+++ b/packages/instrumentation-openai/package.json
@@ devDependencies
-    "openai": "5.12.2",
+    "openai": "^5.12.2",

Optional engines snippet:

{
  "engines": {
    "node": ">=18"
  }
}
🤖 Prompt for AI Agents
In packages/instrumentation-openai/package.json around line 58, the
devDependency "openai" is pinned to an exact version ("5.12.2"); change it to a
caret range (e.g., "^5.12.2") so minor/patch updates within v5 are picked up,
and optionally add an "engines" field with "node": ">=18" to make the Node.js
requirement explicit for local development.

Copy link
Member

Choose a reason for hiding this comment

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

+1 @hmnzr

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

"ts-mocha": "^11.1.0"
},
"homepage": "https://github.com/traceloop/openllmetry-js/tree/main/packages/instrumentation-openai",
Expand Down
8 changes: 4 additions & 4 deletions packages/instrumentation-openai/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
CONTEXT_KEY_ALLOW_TRACE_CONTENT,
SpanAttributes,
} from "@traceloop/ai-semantic-conventions";
import { OpenAIInstrumentationConfig } from "./types";
import { OpenAIVersion, OpenAIInstrumentationConfig } from "./types";
import type {
ChatCompletion,
ChatCompletionChunk,
Expand Down Expand Up @@ -119,7 +119,7 @@ export class OpenAIInstrumentation extends InstrumentationBase {
protected init(): InstrumentationModuleDefinition {
const module = new InstrumentationNodeModuleDefinition(
"openai",
[">=3.1.0 <5"],
[">=3.1.0 <6"],
Copy link

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Widened module semver range to >=3.1.0 <6 — verify repo-wide alignment (docs, deps, tests)

Ensure we don’t have stale references to "<5" elsewhere and that CI/tests cover v5.

Run the following to find and update any lingering constraints and to surface any openai dependency ranges:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Searching for legacy range '>=3.1.0 <5' across the repo..."
rg -n ">=3\.1\.0\s*<5" || true

echo
echo "Searching for 'openai' version constraints in package.json files..."
fd -a package.json | while read -r f; do
  hits=$(rg -n '"openai"\s*:\s*"[^\"]+"' "$f" || true)
  if [[ -n "$hits" ]]; then
    echo "$f"
    echo "$hits"
    echo
  fi
done

echo "Checking docs/workflows for hardcoded SDK support statements..."
rg -n "(OpenAI SDK|openai@|>=3\.1\.0\s*<5|<5)" --glob '*.md' --glob '.github/**/*.yml' || true

Length of output: 5204


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Inspecting instrumentation-openai package.json for openai dependency entries..."
grep -R '"openai"' -n packages/instrumentation-openai/package.json || true

Length of output: 300


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Displaying lines 40-80 from packages/instrumentation-openai/package.json to inspect dependency sections..."
sed -n '40,80p' packages/instrumentation-openai/package.json

Length of output: 1173


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Listing all GitHub workflow files:"
fd -a -e yml .github/workflows

echo
echo "Scanning workflows for 'openai' references:"
rg -n "openai" .github/workflows

echo
echo "Scanning workflows for matrix definitions:"
rg -n "matrix" .github/workflows

Length of output: 505


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Inspecting peerDependencies in instrumentation-openai package.json..."
grep -R '"peerDependencies"' -n packages/instrumentation-openai/package.json || true
sed -n '1,39p' packages/instrumentation-openai/package.json

echo
echo "Checking for GitHub workflow files..."
if [ -d ".github/workflows" ]; then
  find .github/workflows -type f -name '*.yml'
else
  echo ".github/workflows directory not found"
fi

echo
echo "Scanning workflows for 'matrix' or 'openai' references..."
grep -R -n -E 'matrix|openai' .github/workflows || true

Length of output: 1578


Ensure OpenAI v5 is covered in tests, deps, and CI

  • ✅ No legacy >=3.1.0 <5 ranges found in code or docs.
  • packages/instrumentation-openai/package.json
    • devDependencies still pin "openai": "4.38.3" (tests only exercise v4).
    • No peerDependencies entry for openai—add:
      "peerDependencies": {
        "openai": ">=3.1.0 <6"
      }
  • CI workflows (.github/workflows/ci.yml) have no matrix for multiple openai versions.
  • After adding peerDeps and CI matrix, update devDependency or use npm install openai@${{ matrix.version }} in the test job.
🤖 Prompt for AI Agents
In packages/instrumentation-openai/src/instrumentation.ts around line 122, tests
and package metadata don’t cover OpenAI v5: add a peerDependencies entry in
packages/instrumentation-openai/package.json with "openai": ">=3.1.0 <6"; stop
pinning openai in devDependencies (or update it to a range that includes v5) so
local dev isn’t the only source of truth; modify .github/workflows/ci.yml to add
a job matrix for openai versions (at minimum 4.x and 5.x) and in the test job
run npm install openai@${{ matrix.version }} (or use npm ci after adjusting
package.json) so CI actually runs tests against both versions; ensure tests pass
with v5 and adjust any version-specific mocks or imports accordingly.

this.patch.bind(this),
this.unpatch.bind(this),
);
Expand Down Expand Up @@ -213,7 +213,7 @@ export class OpenAIInstrumentation extends InstrumentationBase {

private patchOpenAI(
type: "chat" | "completion",
version: "v3" | "v4" = "v4",
version: OpenAIVersion = "v4",
) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const plugin = this;
Expand Down Expand Up @@ -602,7 +602,7 @@ export class OpenAIInstrumentation extends InstrumentationBase {

private _wrapPromise<T>(
type: "chat" | "completion",
version: "v3" | "v4",
version: OpenAIVersion,
span: Span,
promise: APIPromise<T>,
): APIPromise<T> {
Expand Down
2 changes: 2 additions & 0 deletions packages/instrumentation-openai/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { InstrumentationConfig } from "@opentelemetry/instrumentation";

export type OpenAIVersion = "v3" | "v4" | "v5";

export type ImageUploadCallback = (
traceId: string,
spanId: string,
Expand Down
Loading
Loading