Skip to content

Commit fe47bef

Browse files
sabrennergmmeyer
andauthored
fix: add to @openai/agents blocklist for IITM exclusions (#6452)
* the loader is broken for openai agents, adds the shim for it as well * adds a test * fix esbuild integration test package.json * remove from esbuild package.json * alphabetical order * fix test to actually test --------- Co-authored-by: Greg Meyer <greg@zingage.com>
1 parent 258deeb commit fe47bef

File tree

5 files changed

+32
-11
lines changed

5 files changed

+32
-11
lines changed

initialize.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ${result.source}`
3636
const [NODE_MAJOR, NODE_MINOR] = process.versions.node.split('.').map(Number)
3737

3838
const brokenLoaders = NODE_MAJOR === 18 && NODE_MINOR === 0
39-
const iitmExclusions = [/langsmith/, /openai\/_shims/, /openai\/resources\/chat\/completions\/messages/]
39+
const iitmExclusions = [/langsmith/, /openai\/_shims/, /openai\/resources\/chat\/completions\/messages/, /openai\/agents-core\/dist\/shims/]
4040

4141
export async function load (url, context, nextLoad) {
4242
const iitmExclusionsMatch = iitmExclusions.some((exclusion) => exclusion.test(url))

packages/datadog-plugin-openai/test/integration-test/client.spec.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
FakeAgent,
55
createSandbox,
66
checkSpansForServiceName,
7-
spawnPluginIntegrationTestProc
7+
spawnPluginIntegrationTestProc,
88
} = require('../../../../integration-tests/helpers')
99
const { withVersions } = require('../../../dd-trace/test/setup/mocha')
1010
const { assert } = require('chai')
@@ -17,11 +17,19 @@ describe('esm', () => {
1717
// limit v4 tests while the IITM issue is resolved or a workaround is introduced
1818
// this is only relevant for `openai` >=4.0 <=4.1
1919
// issue link: https://github.com/DataDog/import-in-the-middle/issues/60
20-
withVersions('openai', 'openai', '>=3 <4.0.0 || >4.1.0', version => {
20+
withVersions('openai', 'openai', '>=3 <4.0.0 || >4.1.0', (version) => {
2121
before(async function () {
2222
this.timeout(20000)
23-
sandbox = await createSandbox([`'openai@${version}'`, 'nock'], false, [
24-
'./packages/datadog-plugin-openai/test/integration-test/*'])
23+
sandbox = await createSandbox(
24+
[
25+
`'openai@${version}'`,
26+
'nock',
27+
'@openai/agents',
28+
'@openai/agents-core',
29+
],
30+
false,
31+
['./packages/datadog-plugin-openai/test/integration-test/*']
32+
)
2533
})
2634

2735
after(async () => {
@@ -41,12 +49,21 @@ describe('esm', () => {
4149
const res = agent.assertMessageReceived(({ headers, payload }) => {
4250
assert.propertyVal(headers, 'host', `127.0.0.1:${agent.port}`)
4351
assert.isArray(payload)
44-
assert.strictEqual(checkSpansForServiceName(payload, 'openai.request'), true)
52+
assert.strictEqual(
53+
checkSpansForServiceName(payload, 'openai.request'),
54+
true
55+
)
4556
})
4657

47-
proc = await spawnPluginIntegrationTestProc(sandbox.folder, 'server.mjs', agent.port, null, {
48-
NODE_OPTIONS: '--import dd-trace/register.js'
49-
})
58+
proc = await spawnPluginIntegrationTestProc(
59+
sandbox.folder,
60+
'server.mjs',
61+
agent.port,
62+
null,
63+
{
64+
NODE_OPTIONS: '--import dd-trace/initialize.mjs',
65+
}
66+
)
5067

5168
await res
5269
}).timeout(20000)

packages/datadog-plugin-openai/test/integration-test/server.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import 'dd-trace/init.js'
21
import OpenAI from 'openai'
32

3+
// load in openai agents to test iitm functionality
4+
import { Agent, run } from '@openai/agents' // eslint-disable-line no-unused-vars
5+
46
const params = {
57
model: 'gpt-3.5-turbo-instruct',
68
prompt: 'Hello, OpenAI!',

packages/dd-trace/test/plugins/versions/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"private": true,
66
"dependencies": {
77
"@ai-sdk/openai": "2.0.27",
8+
"@openai/agents": "0.1.0",
9+
"@openai/agents-core": "0.1.0",
810
"@apollo/gateway": "2.11.2",
911
"@apollo/server": "5.0.0",
1012
"@apollo/subgraph": "2.11.2",

register.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ const { register } = require('node:module')
66
const { pathToFileURL } = require('node:url')
77

88
register('./loader-hook.mjs', pathToFileURL(__filename), {
9-
data: { exclude: [/langsmith/, /openai\/_shims/, /openai\/resources\/chat\/completions\/messages/] }
9+
data: { exclude: [/langsmith/, /openai\/_shims/, /openai\/resources\/chat\/completions\/messages/, /openai\/agents-core\/dist\/shims/] }
1010
})

0 commit comments

Comments
 (0)