Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Openai Openai node FinalChatCompletion

From Leeroopedia
Knowledge Sources
Domains Function_Calling, Agentic_Patterns
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for awaiting the final response from a tool execution loop provided by the openai-node SDK.

Description

The finalChatCompletion() method on AbstractChatCompletionRunner returns a promise that resolves to the last ChatCompletion after the tool loop completes. The messages property provides the full conversation history array. These are inherited by both ChatCompletionRunner and ChatCompletionStreamingRunner.

Usage

Call runner.finalChatCompletion() after creating a runner via runTools(). The promise resolves when all tool calls are handled and the model produces its final response.

Code Reference

Source Location

  • Repository: openai-node
  • File: src/lib/AbstractChatCompletionRunner.ts
  • Lines: L78-83 (finalChatCompletion), L37 (messages property)

Signature

export abstract class AbstractChatCompletionRunner<
  Events extends CustomEvents,
  ParsedT,
> extends EventStream<Events> {
  messages: ChatCompletionMessageParam[];

  async finalChatCompletion(): Promise<ParsedChatCompletion<ParsedT>>;
}

Import

import OpenAI from 'openai';
// Access via runner instance from client.chat.completions.runTools(...)

I/O Contract

Inputs

Name Type Required Description
(runner) ChatCompletionStreamingRunner Yes Active runner from runTools()

Outputs

Name Type Description
finalChatCompletion() Promise<ParsedChatCompletion<ParsedT>> The last ChatCompletion from the tool loop
messages ChatCompletionMessageParam[] Full conversation history with tool calls and results

Usage Examples

Awaiting Final Response

import OpenAI from 'openai';

const client = new OpenAI();

const runner = client.chat.completions.runTools({
  model: 'gpt-4o',
  messages: [{ role: 'user', content: 'Look up the weather in London.' }],
  tools: [weatherTool],
});

// Await the final response
const completion = await runner.finalChatCompletion();
console.log(completion.choices[0].message.content);

// Access full conversation history
console.log('Total messages:', runner.messages.length);
for (const msg of runner.messages) {
  console.log(`[${msg.role}]: ${JSON.stringify(msg.content).slice(0, 80)}`);
}

Related Pages

Implements Principle

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment