Skip to content

Commit 3f0069c

Browse files
authored
fix(core): change response metadata type mismatch (#9010)
1 parent b647c28 commit 3f0069c

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

libs/langchain-core/src/messages/ai.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ export class AIMessage<TStructure extends MessageStructure = MessageStructure>
146146

147147
get contentBlocks(): Array<ContentBlock.Standard> {
148148
if (
149+
this.response_metadata &&
149150
"output_version" in this.response_metadata &&
150151
this.response_metadata.output_version === "v1"
151152
) {
152153
return this.content as Array<ContentBlock.Standard>;
153154
}
154155

155156
if (
157+
this.response_metadata &&
156158
"model_provider" in this.response_metadata &&
157159
typeof this.response_metadata.model_provider === "string"
158160
) {
@@ -369,13 +371,15 @@ export class AIMessageChunk<
369371

370372
get contentBlocks(): Array<ContentBlock.Standard> {
371373
if (
374+
this.response_metadata &&
372375
"output_version" in this.response_metadata &&
373376
this.response_metadata.output_version === "v1"
374377
) {
375378
return this.content as Array<ContentBlock.Standard>;
376379
}
377380

378381
if (
382+
this.response_metadata &&
379383
"model_provider" in this.response_metadata &&
380384
typeof this.response_metadata.model_provider === "string"
381385
) {

libs/langchain-core/src/messages/base.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,9 +413,9 @@ export function isOpenAIToolCallArray(
413413

414414
export function _mergeDicts(
415415
// eslint-disable-next-line @typescript-eslint/no-explicit-any
416-
left: Record<string, any>,
416+
left: Record<string, any> = {},
417417
// eslint-disable-next-line @typescript-eslint/no-explicit-any
418-
right: Record<string, any>
418+
right: Record<string, any> = {}
419419
// eslint-disable-next-line @typescript-eslint/no-explicit-any
420420
): Record<string, any> {
421421
const merged = { ...left };

libs/langchain-core/src/messages/message.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -390,21 +390,21 @@ export interface StandardMessageStructure extends MessageStructure {
390390
/** Properties specific to AI messages */
391391
ai: {
392392
/** Metadata about the AI model response */
393-
response_metadata?: ResponseMetadata;
393+
response_metadata: ResponseMetadata;
394394
/** Usage statistics for the AI response */
395-
usage_metadata?: UsageMetadata;
395+
usage_metadata: UsageMetadata;
396396
};
397397
human: {
398398
/** Metadata about the human message */
399-
response_metadata?: Record<string, unknown>;
399+
response_metadata: Record<string, unknown>;
400400
};
401401
system: {
402402
/** Metadata about the system message */
403-
response_metadata?: Record<string, unknown>;
403+
response_metadata: Record<string, unknown>;
404404
};
405405
tool: {
406406
/** Metadata about the tool message */
407-
response_metadata?: Record<string, unknown>;
407+
response_metadata: Record<string, unknown>;
408408
};
409409
};
410410
}
@@ -650,7 +650,7 @@ export type $InferResponseMetadata<
650650
TRole,
651651
"response_metadata"
652652
> extends infer P
653-
? [P] extends [unknown]
653+
? [P] extends [never]
654654
? Record<string, unknown>
655655
: P
656656
: never;

libs/langchain-core/src/messages/tests/messages.test-d.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
$InferMessageContentBlocks,
1515
$InferMessageProperty,
1616
$InferMessageProperties,
17+
$InferResponseMetadata,
1718
} from "../message.js";
1819
import { ContentBlock } from "../content/index.js";
1920
import { ResponseMetadata, UsageMetadata } from "../metadata.js";
@@ -617,17 +618,17 @@ describe("$NormalizedMessageStructure<T>", () => {
617618
// Properties backfilled for all standard roles
618619
type AIP = NonNullable<N["properties"]>["ai"];
619620
expectTypeOf<AIP>().toEqualTypeOf<{
620-
response_metadata?: ResponseMetadata | undefined;
621-
usage_metadata?: UsageMetadata | undefined;
621+
response_metadata: ResponseMetadata;
622+
usage_metadata: UsageMetadata;
622623
}>();
623624
expectTypeOf<NonNullable<N["properties"]>["human"]>().toEqualTypeOf<{
624-
response_metadata?: Record<string, unknown> | undefined;
625+
response_metadata: Record<string, unknown>;
625626
}>();
626627
expectTypeOf<NonNullable<N["properties"]>["system"]>().toEqualTypeOf<{
627-
response_metadata?: Record<string, unknown> | undefined;
628+
response_metadata: Record<string, unknown>;
628629
}>();
629630
expectTypeOf<NonNullable<N["properties"]>["tool"]>().toEqualTypeOf<{
630-
response_metadata?: Record<string, unknown> | undefined;
631+
response_metadata: Record<string, unknown>;
631632
}>();
632633
});
633634
});
@@ -840,23 +841,23 @@ describe("$InferMessageProperties<TStructure, TRole>", () => {
840841

841842
type AIProps = $InferMessageProperties<S, "ai">;
842843
expectTypeOf<AIProps>().toEqualTypeOf<{
843-
response_metadata?: ResponseMetadata | undefined;
844-
usage_metadata?: UsageMetadata | undefined;
844+
response_metadata: ResponseMetadata;
845+
usage_metadata: UsageMetadata;
845846
}>();
846847

847848
type HumanProps = $InferMessageProperties<S, "human">;
848849
expectTypeOf<HumanProps>().toEqualTypeOf<{
849-
response_metadata?: Record<string, unknown> | undefined;
850+
response_metadata: Record<string, unknown>;
850851
}>();
851852

852853
type SystemProps = $InferMessageProperties<S, "system">;
853854
expectTypeOf<SystemProps>().toEqualTypeOf<{
854-
response_metadata?: Record<string, unknown> | undefined;
855+
response_metadata: Record<string, unknown>;
855856
}>();
856857

857858
type ToolProps = $InferMessageProperties<S, "tool">;
858859
expectTypeOf<ToolProps>().toEqualTypeOf<{
859-
response_metadata?: Record<string, unknown> | undefined;
860+
response_metadata: Record<string, unknown>;
860861
}>();
861862
});
862863

@@ -871,12 +872,12 @@ describe("$InferMessageProperties<TStructure, TRole>", () => {
871872
type HumanProps = $InferMessageProperties<S, "human">;
872873

873874
expectTypeOf<AIProps>().toEqualTypeOf<{
874-
response_metadata: ResponseMetadata | undefined;
875-
usage_metadata: UsageMetadata | undefined;
875+
response_metadata: ResponseMetadata;
876+
usage_metadata: UsageMetadata;
876877
foo: { bar: number };
877878
}>();
878879
expectTypeOf<HumanProps>().toEqualTypeOf<{
879-
response_metadata: Record<string, unknown> | undefined;
880+
response_metadata: Record<string, unknown>;
880881
metadata: { qux: string };
881882
}>();
882883
});
@@ -895,8 +896,8 @@ describe("$InferMessageProperties<TStructure, TRole>", () => {
895896
type AIProps = $InferMessageProperties<S, "ai">;
896897

897898
expectTypeOf<AIProps>().toEqualTypeOf<{
898-
response_metadata: ResponseMetadata | undefined;
899-
usage_metadata: UsageMetadata | undefined;
899+
response_metadata: ResponseMetadata;
900+
usage_metadata: UsageMetadata;
900901
keep: boolean;
901902
nested: { a: number };
902903
}>();
@@ -929,8 +930,8 @@ describe("$InferMessageProperties<TStructure, TRole>", () => {
929930
type AIProps = $InferMessageProperties<M, "ai">;
930931

931932
expectTypeOf<AIProps>().toEqualTypeOf<{
932-
response_metadata: ResponseMetadata | undefined;
933-
usage_metadata: UsageMetadata | undefined;
933+
response_metadata: ResponseMetadata;
934+
usage_metadata: UsageMetadata;
934935
foo: { bar: string; baz: number };
935936
baz: { qux: string };
936937
}>();
@@ -969,6 +970,10 @@ describe("$InferMessageProperty<TStructure, TRole, K>", () => {
969970

970971
describe("$InferResponseMetadata<TStructure, TRole>", () => {
971972
// TODO(hntrl): implement
973+
test("should return `ResponseMetadata | undefined` when using `<MessageStructure, 'ai'>`", async () => {
974+
type AIResponseMetadata = $InferResponseMetadata<MessageStructure, "ai">;
975+
expectTypeOf<AIResponseMetadata>().toEqualTypeOf<ResponseMetadata>();
976+
});
972977
});
973978

974979
describe("Message", () => {

libs/langchain-core/src/messages/tool.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ export class ToolMessageChunk<
190190

191191
export interface ToolCall<
192192
TName extends string = string,
193-
TArgs extends Record<string, unknown> = Record<string, unknown>
193+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
194+
TArgs extends Record<string, any> = Record<string, any>
194195
> {
195196
readonly type?: "tool_call";
196197
/**

0 commit comments

Comments
 (0)