Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/clever-needles-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@openai/agents-core": patch
"@openai/agents-openai": patch
---

Add OpenAI Response object on ResponseSpanData for other exporters.
1 change: 1 addition & 0 deletions packages/agents-core/src/tracing/spans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type ResponseSpanData = SpanDataBase & {
* Not used by the OpenAI tracing provider but helpful for other tracing providers.
*/
_input?: string | Record<string, any>[];
_response?: Record<string, any>;
};

export type HandoffSpanData = SpanDataBase & {
Expand Down
31 changes: 30 additions & 1 deletion packages/agents-core/test/tracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ import {

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

import { Span, CustomSpanData, NoopSpan } from '../src/tracing/spans';
import {
Span,
CustomSpanData,
ResponseSpanData,
NoopSpan,
} from '../src/tracing/spans';

import {
BatchTraceProcessor,
Expand Down Expand Up @@ -301,3 +306,27 @@ describe('TraceProvider disabled behaviour', () => {
expect(span).toBeInstanceOf(NoopSpan);
});
});

// -----------------------------------------------------------------------------------------
// Tests for ResponseSpanData serialization
// -----------------------------------------------------------------------------------------

describe('ResponseSpanData serialization', () => {
it('removes private fields _input and _response from JSON output', () => {
const data: ResponseSpanData = {
type: 'response',
response_id: 'resp_123',
_input: 'private input data',
_response: { id: 'response_obj' } as any,
};

const span = new Span({ traceId: 'trace_123', data }, new TestProcessor());

const json = span.toJSON() as any;

expect(json.span_data.type).toBe('response');
expect(json.span_data.response_id).toBe('resp_123');
expect(json.span_data).not.toHaveProperty('_input');
expect(json.span_data).not.toHaveProperty('_response');
});
});
8 changes: 4 additions & 4 deletions packages/agents-openai/src/openaiResponsesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,8 @@ export class OpenAIResponsesModel implements Model {

if (request.tracing) {
span.spanData.response_id = response.id;
if (request.tracing === true) {
span.spanData._input = request.input;
}
span.spanData._input = request.input;
span.spanData._response = response;
}

return response;
Expand Down Expand Up @@ -931,8 +930,9 @@ export class OpenAIResponsesModel implements Model {
};
}

if (span && finalResponse && request.tracing) {
if (request.tracing && span && finalResponse) {
span.spanData.response_id = finalResponse.id;
span.spanData._response = finalResponse;
}
} catch (error) {
if (span) {
Expand Down