Skip to content
Draft
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
58 changes: 57 additions & 1 deletion packages/agents-openai/src/openaiChatCompletionsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@
async *getStreamedResponse(
request: ModelRequest,
): AsyncIterable<ResponseStreamEvent> {
const span = request.tracing ? createGenerationSpan() : undefined;
const span = request.tracing
? createGenerationSpan({
data: {
model: this.#model,
model_config: request.modelSettings,
},
})
: undefined;
try {
if (span) {
span.start();
Expand Down Expand Up @@ -218,6 +225,55 @@
event.type === 'response_done' &&
response.usage?.total_tokens === 0
) {
response.choices = event.response.output
.flatMap((o): OpenAI.Chat.Completions.ChatCompletion.Choice[] => {

Check failure on line 229 in packages/agents-openai/src/openaiChatCompletionsModel.ts

View workflow job for this annotation

GitHub Actions / test (24.3.x)

test/openaiChatCompletionsModel.test.ts > OpenAIChatCompletionsModel > populates usage from response_done event when initial usage is zero

TypeError: Cannot read properties of undefined (reading 'flatMap') ❯ OpenAIChatCompletionsModel.getStreamedResponse src/openaiChatCompletionsModel.ts:229:14 ❯ test/openaiChatCompletionsModel.test.ts:584:24 ❯ ../agents-core/src/tracing/context.ts:61:20 ❯ test/openaiChatCompletionsModel.test.ts:583:5

Check failure on line 229 in packages/agents-openai/src/openaiChatCompletionsModel.ts

View workflow job for this annotation

GitHub Actions / test (20)

test/openaiChatCompletionsModel.test.ts > OpenAIChatCompletionsModel > populates usage from response_done event when initial usage is zero

TypeError: Cannot read properties of undefined (reading 'flatMap') ❯ OpenAIChatCompletionsModel.getStreamedResponse src/openaiChatCompletionsModel.ts:229:14 ❯ test/openaiChatCompletionsModel.test.ts:584:24 ❯ ../agents-core/src/tracing/context.ts:61:20 ❯ test/openaiChatCompletionsModel.test.ts:583:5
if (o.type === 'function_call') {
Copy link
Member

Choose a reason for hiding this comment

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

The logic here requires updates when the platform adds new types. We'd like to explore more robust way to handle the conversion here.

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, I was worried about that too. Do you have any suggestions? This is the first time I've actually looked into this API!

return [
{
finish_reason: 'tool_calls',
index: 0,
logprobs: null,
message: {
content: null,
refusal: null,
role: 'assistant',
tool_calls: [
{
type: 'function',
function: {
arguments: o.arguments,
name: o.name,
},
id: o.callId,
},
],
},
},
];
} else if (o.type === 'message') {
return o.content
.filter((c) => c.type === 'output_text')
.map((c) => {
return {
finish_reason: 'stop',
index: 0,
logprobs: null,
message: {
content: c.text,
refusal: null,
role: 'assistant',
},
};
});
} else {
return [];
}
})
.map((choice, index) => ({
...choice,
index,
}));

response.usage = {
prompt_tokens: event.response.usage.inputTokens,
completion_tokens: event.response.usage.outputTokens,
Expand Down
Loading