Append Messages langbase.threads.append()

You can use the threads.append() function to add new messages to an existing thread. This helps you maintain conversation history and build interactive chat experiences.


Generate a User/Org API key

You will need to generate an API key to authenticate your requests. For more information, visit the User/Org API key documentation.


API reference

langbase.threads.append(options)

Add new messages to an existing thread.

Function Signature

langbase.threads.append(options);

// with types
langbase.threads.append(options: ThreadsCreate);

options

  • Name
    options
    Type
    ThreadMessagesCreate
    Description

    ThreadMessagesCreate Object

    interface ThreadMessagesCreate {
    	threadId: string;
    	messages: ThreadMessage[];
    }
    
    interface ThreadMessage extends Message {
    	attachments?: any[];
    	metadata?: Record<string, string>;
    }
    
    interface Message {
    	role: 'user' | 'assistant' | 'system' | 'tool';
    	content: string | null;
    	name?: string;
    	tool_call_id?: string;
    	tool_calls?: ToolCall[];
    }
    
    interface ToolCall {
    	id: string;
    	type: 'function';
    	function: Function;
    }
    
    interface Function {
    	name: string;
    	arguments: string;
    }
    

Usage example

Install the SDK

npm i langbase

Environment variables

.env file

LANGBASE_API_KEY="<USER/ORG-API-KEY>"

langbase.threads.append() example

Append Example

const messages = await langbase.threads.append({
	threadId: "thread_125",
	messages: [{
		role: "assistant",
		content: "How can I help you today?"
	}]
});

Response

The response of the threads.append() function is a promise that resolves to an array of ThreadMessagesBaseResponse objects.

ThreadMessagesBaseResponse

export interface ThreadMessagesBaseResponse {
	id: string;
	created_at: number;
	thread_id: string;
	content: string;
	role: Role;
	tool_call_id: string | null;
	tool_calls: ToolCall[] | [];
	name: string | null;
	attachments: any[] | [];
	metadata: Record<string, string> | {};
}

Response Example

[
	{
		"id": "msg_124",
		"thread_id": "thread_123",
		"created_at": 1709544120,
		"role": "user",
		"content": "Hello, I need help!",
		"tool_call_id": null,
		"tool_calls": [],
		"name": null,
		"attachments": [],
		"metadata": {}
	},
	{
		"id": "msg_125",
		"thread_id": "thread_123",
		"created_at": 1709544120,
		"role": "assistant",
		"content": "How can I help you today?",
		"tool_call_id": null,
		"tool_calls": [],
		"name": null,
		"attachments": [],
		"metadata": {}
	}
]