Skip to content

Commit 45c1eb3

Browse files
test(agents-openai): verify unicode export
1 parent f61fd18 commit 45c1eb3

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

packages/agents-openai/src/openaiTracingExporter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ export class OpenAITracingExporter implements TracingExporter {
5555
data: items.map((items) => items.toJSON()).filter((item) => !!item),
5656
};
5757

58+
const body = Buffer.from(JSON.stringify(payload), 'utf8');
59+
5860
let attempts = 0;
5961
let delay = this.#options.baseDelay;
6062

@@ -68,7 +70,7 @@ export class OpenAITracingExporter implements TracingExporter {
6870
'OpenAI-Beta': 'traces=v1',
6971
...HEADERS,
7072
},
71-
body: JSON.stringify(payload),
73+
body,
7274
signal,
7375
});
7476

packages/agents-openai/test/openaiTracingExporter.test.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,39 @@ describe('OpenAITracingExporter', () => {
6363
...HEADERS,
6464
}),
6565
);
66-
expect(JSON.parse(opts.body as string)).toEqual({ data: [item.toJSON()] });
66+
const body = Buffer.isBuffer(opts.body)
67+
? opts.body.toString()
68+
: (opts.body as string);
69+
expect(JSON.parse(body)).toEqual({ data: [item.toJSON()] });
70+
});
71+
72+
it('exports unicode payload via fetch', async () => {
73+
const unicodeSpan = createCustomSpan({
74+
data: {
75+
name: 'unicode',
76+
},
77+
});
78+
unicodeSpan.toJSON = () => ({
79+
object: 'trace.span',
80+
id: 'u123',
81+
trace_id: 'u123',
82+
parent_id: 'u123',
83+
started_at: '1',
84+
ended_at: '2',
85+
span_data: { prompt: 'Hello “world”' },
86+
error: null,
87+
});
88+
89+
const fetchMock = vi.fn().mockResolvedValue({ ok: true });
90+
vi.stubGlobal('fetch', fetchMock);
91+
92+
const exporter = new OpenAITracingExporter({ apiKey: 'key1' });
93+
await exporter.export([unicodeSpan]);
94+
95+
const [, opts] = fetchMock.mock.calls[0];
96+
expect(Buffer.isBuffer(opts.body)).toBe(true);
97+
const body = (opts.body as Buffer).toString();
98+
expect(JSON.parse(body)).toEqual({ data: [unicodeSpan.toJSON()] });
6799
});
68100

69101
it('retries on server errors', async () => {
@@ -91,9 +123,15 @@ describe('OpenAITracingExporter', () => {
91123

92124
it('stops on client error', async () => {
93125
const item = fakeSpan;
94-
const fetchMock = vi.fn().mockResolvedValue({ ok: false, status: 400, text: async () => 'bad' });
126+
const fetchMock = vi
127+
.fn()
128+
.mockResolvedValue({ ok: false, status: 400, text: async () => 'bad' });
95129
vi.stubGlobal('fetch', fetchMock);
96-
const exporter = new OpenAITracingExporter({ apiKey: 'key3', endpoint: 'u', maxRetries: 2 });
130+
const exporter = new OpenAITracingExporter({
131+
apiKey: 'key3',
132+
endpoint: 'u',
133+
maxRetries: 2,
134+
});
97135
await exporter.export([item]);
98136
expect(fetchMock).toHaveBeenCalledTimes(1);
99137
});

0 commit comments

Comments
 (0)