Skip to content

Commit c75fdae

Browse files
authored
Cb/standard outputs (#8587)
2 parents 59eba2c + fe1de53 commit c75fdae

File tree

6 files changed

+111
-179
lines changed

6 files changed

+111
-179
lines changed

langchain-core/src/_standard/_scratchpad.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ if (openaiMessage.type === "user") {
4141
}
4242
}
4343

44+
// @ts-expect-error - demo
45+
const file: ContentBlock.Multimodal.File = {
46+
type: "file",
47+
fileId: "123",
48+
// uncommenting this will cause a type error
49+
// url: "asddas",
50+
mimeType: "image/png",
51+
metadata: {
52+
width: 100,
53+
height: 100,
54+
},
55+
};
56+
4457
function normalReturn(): BaseMessage {
4558
return {
4659
type: "user",
@@ -62,14 +75,14 @@ if (normalMessage.type === "user") {
6275

6376
// Test with multiple tool call interfaces
6477

65-
interface ToolCallA extends ContentBlock.Tools.ToolCallContentBlock {
78+
interface ToolCallA extends ContentBlock.Tools.ToolCall {
6679
name: "tool_a";
6780
args: {
6881
bar: string;
6982
};
7083
}
7184

72-
interface ToolCallB extends ContentBlock.Tools.ToolCallContentBlock {
85+
interface ToolCallB extends ContentBlock.Tools.ToolCall {
7386
name: "tool_b";
7487
args: {
7588
foo: string;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface ContentBlock {
2+
readonly type: string;
3+
}

langchain-core/src/_standard/content/index.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import { ToolContentBlocks } from "./tools.js";
2-
import { MultimodalContentBlocks } from "./multimodal.js";
3-
4-
export interface ContentBlock {
5-
readonly type: string;
6-
}
1+
import { ContentBlock as BaseContentBlock } from "./base.js";
2+
import { Tools as ToolsContentBlock } from "./tools.js";
3+
import { Multimodal as MultimodalContentBlock } from "./multimodal.js";
74

85
// eslint-disable-next-line @typescript-eslint/no-namespace
96
export declare namespace ContentBlock {
107
/** Content block for citation */
11-
interface Citation extends ContentBlock {
8+
interface Citation extends BaseContentBlock {
129
/**
1310
* Type of the content block
1411
*/
@@ -44,7 +41,7 @@ export declare namespace ContentBlock {
4441
}
4542

4643
/** Content block for text */
47-
interface TextContentBlock extends ContentBlock {
44+
interface Text extends BaseContentBlock {
4845
/**
4946
* Type of the content block
5047
*/
@@ -66,7 +63,7 @@ export declare namespace ContentBlock {
6663
}
6764

6865
/** Content block for reasoning output */
69-
interface ReasoningContentBlock extends ContentBlock {
66+
interface Reasoning extends BaseContentBlock {
7067
/**
7168
* Type of the content block
7269
*/
@@ -104,12 +101,11 @@ export declare namespace ContentBlock {
104101
signature?: string;
105102
}
106103

107-
export import Tools = ToolContentBlocks;
108-
export import Multimodal = MultimodalContentBlocks;
109-
110-
export type Standard =
111-
| TextContentBlock
112-
| ReasoningContentBlock
113-
| Tools.Standard
114-
| Multimodal.Standard;
104+
export import Tools = ToolsContentBlock;
105+
export import Multimodal = MultimodalContentBlock;
106+
export type Types =
107+
| Text
108+
| Reasoning
109+
| Tools.ContentBlock
110+
| Multimodal.ContentBlock;
115111
}
Lines changed: 56 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +1,84 @@
1-
import { ContentBlock } from "./index.js";
1+
import { ContentBlock as BaseContentBlock } from "./base.js";
22

33
// eslint-disable-next-line @typescript-eslint/no-namespace
4-
export declare namespace MultimodalContentBlocks {
5-
/** Content block for multimodal data */
6-
export interface DataContentBlock<
7-
TMetadata extends Record<string, unknown> = Record<string, unknown>
8-
> extends ContentBlock {
4+
export declare namespace Multimodal {
5+
type DataRecordFileId = {
6+
/**
7+
* ID of the image file. e.g. from the OpenAI file api
8+
*/
9+
fileId: string;
10+
url?: never;
11+
data?: never;
12+
};
13+
type DataRecordUrl = {
14+
fileId?: never;
915
/**
10-
* MIME type of the file. Required for base64 encoding.
16+
* URL of the data file
1117
*/
12-
mimeType?: string;
18+
url: string;
19+
data?: never;
20+
};
21+
type DataRecordData = {
22+
fileId?: never;
23+
url?: never;
1324
/**
14-
* Metadata of the file
25+
* Base64 encoded string or binary data of the data
1526
*/
16-
metadata?: TMetadata;
17-
}
27+
data: string | Uint8Array;
28+
};
29+
type DataRecord = DataRecordFileId | DataRecordUrl | DataRecordData;
30+
31+
/** Content block for multimodal data */
32+
export type Data<
33+
TMetadata extends Record<string, unknown> = Record<string, unknown>
34+
> = BaseContentBlock &
35+
DataRecord & {
36+
/**
37+
* MIME type of the file. Required for base64 encoding.
38+
*/
39+
mimeType?: string;
40+
/**
41+
* Metadata of the file
42+
*/
43+
metadata?: TMetadata;
44+
/**
45+
* Content block identifier for multimodal content, e.g. image, video, audio, file or plain text. This can be either:
46+
* - generated by the provider (e.g., an OpenAI block ID)
47+
* - generated by LangChain upon creation
48+
*/
49+
id?: string;
50+
};
1851

1952
/** Content block for image data */
20-
export interface ImageContentBlock extends DataContentBlock {
53+
export type Image = Data & {
2154
/**
2255
* Type of the content block
2356
*/
2457
readonly type: "image";
25-
/**
26-
* Image content block identifier, which can be either
27-
* - generated by the provider (e.g., an OpenAI image block ID)
28-
* - generated by LangChain upon creation
29-
*/
30-
id?: string;
31-
/**
32-
* ID of the image file. e.g. from the OpenAI file api
33-
*/
34-
fileId?: string;
35-
/**
36-
* MIME type of the image file. Required for base64 encoding.
37-
*/
38-
mimeType?: string;
39-
/**
40-
* URL of the image file
41-
*/
42-
url?: string;
43-
/**
44-
* Base64 encoded data of the image file
45-
*/
46-
base64?: string;
47-
}
58+
};
4859
/** Content block for video data */
49-
export interface VideoContentBlock extends DataContentBlock {
60+
export type Video = Data & {
5061
/**
5162
* Type of the content block
5263
*/
5364
readonly type: "video";
54-
/**
55-
* Video content block identifier, which can be either
56-
* - generated by the provider (e.g., an OpenAI video block ID)
57-
* - generated by LangChain upon creation
58-
*/
59-
id?: string;
60-
/**
61-
* ID of the video file. e.g. from the OpenAI file api
62-
*/
63-
fileId?: string;
64-
/**
65-
* MIME type of the video file. Required for base64 encoding.
66-
*/
67-
mimeType?: string;
68-
/**
69-
* URL of the video file
70-
*/
71-
url?: string;
72-
/**
73-
* Base64 encoded data of the video file
74-
*/
75-
base64?: string;
76-
}
65+
};
7766

7867
/** Content block for audio data */
79-
export interface AudioContentBlock extends DataContentBlock {
68+
export type Audio = Data & {
8069
/**
8170
* Type of the content block
8271
*/
8372
readonly type: "audio";
84-
/**
85-
* Audio content block identifier, which can be either
86-
* - generated by the provider (e.g., an OpenAI audio block ID)
87-
* - generated by LangChain upon creation
88-
*/
89-
id?: string;
90-
/**
91-
* ID of the audio file. e.g. from the OpenAI file api
92-
*/
93-
fileId?: string;
94-
/**
95-
* MIME type of the audio file. Required for base64 encoding.
96-
*/
97-
mimeType?: string;
98-
/**
99-
* URL of the audio file
100-
*/
101-
url?: string;
102-
/**
103-
* Base64 encoded data of the audio file
104-
*/
105-
base64?: string;
106-
}
73+
};
10774

10875
/** Content block for plain text data */
109-
export interface PlainTextContentBlock extends DataContentBlock {
76+
export type PlainText = Data & {
11077
/**
11178
* Type of the content block
79+
* maybe only "text"?
11280
*/
11381
readonly type: "text-plain";
114-
/**
115-
* Plain text content block identifier, which can be either
116-
* - generated by the provider (e.g., an OpenAI file ID)
117-
* - generated by LangChain upon creation
118-
*/
119-
id?: string;
120-
/**
121-
* ID of the file. e.g. from the OpenAI file api
122-
*/
123-
fileId?: string;
124-
/**
125-
* MIME type of the file. Required for base64 encoding.
126-
*/
127-
mimeType?: "text/plain";
128-
/**
129-
* URL of the plain text file
130-
*/
131-
url?: string;
132-
/**
133-
* Base64 encoded data of the file
134-
*/
135-
base64?: string;
13682
/**
13783
* Plaintext content. This is optional if the data is base64 encoded.
13884
*/
@@ -145,42 +91,15 @@ export declare namespace MultimodalContentBlocks {
14591
* Context for the text, e.g. a description or a summary of the text's content
14692
*/
14793
context?: string;
148-
}
94+
};
14995

15096
/** Content block for file data */
151-
export interface FileContentBlock extends DataContentBlock {
97+
export type File = Data & {
15298
/**
15399
* Type of the content block
154100
*/
155101
readonly type: "file";
156-
/**
157-
* Search call identifier, which can be either
158-
* - generated by the provider (e.g., an OpenAI search call ID)
159-
* - generated by LangChain upon creation
160-
*/
161-
id?: string;
162-
/**
163-
* ID of the file. e.g. from the OpenAI file api
164-
*/
165-
fileId?: string;
166-
/**
167-
* MIME type of the file. Required for base64 encoding.
168-
*/
169-
mimeType?: string;
170-
/**
171-
* URL of the file
172-
*/
173-
url?: string;
174-
/**
175-
* Base64 encoded data of the file
176-
*/
177-
base64?: string;
178-
}
102+
};
179103

180-
export type Standard =
181-
| ImageContentBlock
182-
| VideoContentBlock
183-
| AudioContentBlock
184-
| PlainTextContentBlock
185-
| FileContentBlock;
104+
export type ContentBlock = Image | Video | Audio | PlainText | File;
186105
}

0 commit comments

Comments
 (0)