Skip to content

Commit e6fad4a

Browse files
authored
fix(google-common): Search grounding formatting (#7471)
1 parent 8ad8547 commit e6fad4a

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

libs/langchain-google-common/src/output_parsers.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export abstract class BaseGoogleSearchOutputParser extends BaseLLMOutputParser<s
2323
): GroundingInfo | undefined {
2424
if ("message" in generation) {
2525
const responseMetadata = generation?.message?.response_metadata;
26-
const metadata = responseMetadata.groundingMetadata;
26+
const metadata = responseMetadata?.groundingMetadata;
2727
const supports =
28-
responseMetadata.groundingSupport ?? metadata.groundingSupports ?? [];
28+
responseMetadata?.groundingSupport ?? metadata?.groundingSupports ?? [];
2929
if (metadata) {
3030
return {
3131
metadata,
@@ -144,7 +144,7 @@ export abstract class BaseGoogleSearchOutputParser extends BaseLLMOutputParser<s
144144
* @param grounding
145145
*/
146146
protected searchSuggestion(grounding: GroundingInfo): string {
147-
return grounding.metadata.searchEntryPoint?.renderedContent ?? "";
147+
return grounding?.metadata?.searchEntryPoint?.renderedContent ?? "";
148148
}
149149

150150
protected annotateText(text: string, grounding: GroundingInfo): string {
@@ -198,7 +198,8 @@ export class SimpleGoogleSearchOutputParser extends BaseGoogleSearchOutputParser
198198

199199
protected textSuffix(_text: string, grounding: GroundingInfo): string {
200200
let ret = "\n";
201-
const chunks: GeminiGroundingChunk[] = grounding.metadata.groundingChunks;
201+
const chunks: GeminiGroundingChunk[] =
202+
grounding?.metadata?.groundingChunks ?? [];
202203
chunks.forEach((chunk, index) => {
203204
ret = `${ret}${this.chunkToString(chunk, index)}\n`;
204205
});

libs/langchain-google-common/src/tests/output_parsers.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,23 @@ describe("GoogleSearchOutputParsers", () => {
214214

215215
expect(result).toEqual(expectation);
216216
});
217+
218+
test("non-grounded", async () => {
219+
const record: Record<string, any> = {};
220+
const projectId = mockId();
221+
const authOptions: MockClientAuthInfo = {
222+
record,
223+
projectId,
224+
resultFile: "chat-1-mock.json",
225+
};
226+
227+
const model = new ChatGoogle({
228+
authOptions,
229+
modelName: "gemini-1.5-pro-002",
230+
});
231+
const parser = new SimpleGoogleSearchOutputParser();
232+
const chain = model.pipe(parser);
233+
const result = await chain.invoke("Flip a coin.");
234+
expect(result).toEqual("T");
235+
});
217236
});

0 commit comments

Comments
 (0)