Skip to content

Commit 0a178dc

Browse files
authored
core[minor]: Add ID field to messages (#5817)
* core[minor]: Add ID field to messages * added id to openai * revert oai change
1 parent 2876d4e commit 0a178dc

File tree

7 files changed

+18
-0
lines changed

7 files changed

+18
-0
lines changed

langchain-core/src/messages/ai.ts

+1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ export class AIMessageChunk extends BaseMessageChunk {
244244
chunk.response_metadata
245245
),
246246
tool_call_chunks: [],
247+
id: this.id ?? chunk.id,
247248
};
248249
if (
249250
this.tool_call_chunks !== undefined ||

langchain-core/src/messages/base.ts

+12
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ export type BaseMessageFields = {
106106
/** Response metadata. For example: response headers, logprobs, token counts. */
107107
// eslint-disable-next-line @typescript-eslint/no-explicit-any
108108
response_metadata?: Record<string, any>;
109+
/**
110+
* An optional unique identifier for the message. This should ideally be
111+
* provided by the provider/model which created the message.
112+
*/
113+
id?: string;
109114
};
110115

111116
export function mergeContent(
@@ -170,6 +175,12 @@ export abstract class BaseMessage
170175
/** Response metadata. For example: response headers, logprobs, token counts. */
171176
response_metadata: NonNullable<BaseMessageFields["response_metadata"]>;
172177

178+
/**
179+
* An optional unique identifier for the message. This should ideally be
180+
* provided by the provider/model which created the message.
181+
*/
182+
id?: string;
183+
173184
/** The type of the message. */
174185
abstract _getType(): MessageType;
175186

@@ -200,6 +211,7 @@ export abstract class BaseMessage
200211
this.content = fields.content;
201212
this.additional_kwargs = fields.additional_kwargs;
202213
this.response_metadata = fields.response_metadata;
214+
this.id = fields.id;
203215
}
204216

205217
toDict(): StoredMessage {

langchain-core/src/messages/chat.ts

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export class ChatMessageChunk extends BaseMessageChunk {
9090
chunk.response_metadata
9191
),
9292
role: this.role,
93+
id: this.id ?? chunk.id,
9394
});
9495
}
9596
}

langchain-core/src/messages/function.ts

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export class FunctionMessageChunk extends BaseMessageChunk {
6969
chunk.response_metadata
7070
),
7171
name: this.name ?? "",
72+
id: this.id ?? chunk.id,
7273
});
7374
}
7475
}

langchain-core/src/messages/human.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class HumanMessageChunk extends BaseMessageChunk {
4343
this.response_metadata,
4444
chunk.response_metadata
4545
),
46+
id: this.id ?? chunk.id,
4647
});
4748
}
4849
}

langchain-core/src/messages/system.ts

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class SystemMessageChunk extends BaseMessageChunk {
4343
this.response_metadata,
4444
chunk.response_metadata
4545
),
46+
id: this.id ?? chunk.id,
4647
});
4748
}
4849
}

langchain-core/src/messages/tool.ts

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export class ToolMessageChunk extends BaseMessageChunk {
8888
chunk.response_metadata
8989
),
9090
tool_call_id: this.tool_call_id,
91+
id: this.id ?? chunk.id,
9192
});
9293
}
9394
}

0 commit comments

Comments
 (0)