Skip to content

Commit 6e1d67d

Browse files
Export OpenAI Response object on ResponseSpanData. (#85)
* Export OpenAI Response object on ResponseSpanData. * Remove dependency on OpenAI types. * Create clever-needles-flow.md --------- Co-authored-by: Dominik Kundel <[email protected]>
1 parent 479825c commit 6e1d67d

File tree

4 files changed

+41
-5
lines changed

4 files changed

+41
-5
lines changed

.changeset/clever-needles-flow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@openai/agents-core": patch
3+
"@openai/agents-openai": patch
4+
---
5+
6+
Add OpenAI Response object on ResponseSpanData for other exporters.

packages/agents-core/src/tracing/spans.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export type ResponseSpanData = SpanDataBase & {
3838
* Not used by the OpenAI tracing provider but helpful for other tracing providers.
3939
*/
4040
_input?: string | Record<string, any>[];
41+
_response?: Record<string, any>;
4142
};
4243

4344
export type HandoffSpanData = SpanDataBase & {

packages/agents-core/test/tracing.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010

1111
import { Trace, NoopTrace } from '../src/tracing/traces';
1212

13-
import { Span, CustomSpanData, NoopSpan } from '../src/tracing/spans';
13+
import {
14+
Span,
15+
CustomSpanData,
16+
ResponseSpanData,
17+
NoopSpan,
18+
} from '../src/tracing/spans';
1419

1520
import {
1621
BatchTraceProcessor,
@@ -301,3 +306,27 @@ describe('TraceProvider disabled behaviour', () => {
301306
expect(span).toBeInstanceOf(NoopSpan);
302307
});
303308
});
309+
310+
// -----------------------------------------------------------------------------------------
311+
// Tests for ResponseSpanData serialization
312+
// -----------------------------------------------------------------------------------------
313+
314+
describe('ResponseSpanData serialization', () => {
315+
it('removes private fields _input and _response from JSON output', () => {
316+
const data: ResponseSpanData = {
317+
type: 'response',
318+
response_id: 'resp_123',
319+
_input: 'private input data',
320+
_response: { id: 'response_obj' } as any,
321+
};
322+
323+
const span = new Span({ traceId: 'trace_123', data }, new TestProcessor());
324+
325+
const json = span.toJSON() as any;
326+
327+
expect(json.span_data.type).toBe('response');
328+
expect(json.span_data.response_id).toBe('resp_123');
329+
expect(json.span_data).not.toHaveProperty('_input');
330+
expect(json.span_data).not.toHaveProperty('_response');
331+
});
332+
});

packages/agents-openai/src/openaiResponsesModel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,8 @@ export class OpenAIResponsesModel implements Model {
839839

840840
if (request.tracing) {
841841
span.spanData.response_id = response.id;
842-
if (request.tracing === true) {
843-
span.spanData._input = request.input;
844-
}
842+
span.spanData._input = request.input;
843+
span.spanData._response = response;
845844
}
846845

847846
return response;
@@ -933,8 +932,9 @@ export class OpenAIResponsesModel implements Model {
933932
};
934933
}
935934

936-
if (span && finalResponse && request.tracing) {
935+
if (request.tracing && span && finalResponse) {
937936
span.spanData.response_id = finalResponse.id;
937+
span.spanData._response = finalResponse;
938938
}
939939
} catch (error) {
940940
if (span) {

0 commit comments

Comments
 (0)