Skip to content

Commit 4b71d80

Browse files
committed
feat!: Support invoke with structured output
1 parent 8d57904 commit 4b71d80

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

packages/ai-providers/server-ai-langchain/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"license": "Apache-2.0",
2929
"devDependencies": {
3030
"@langchain/core": "^0.3.0",
31-
"@launchdarkly/server-sdk-ai": "^0.12.3",
31+
"@launchdarkly/server-sdk-ai": "^0.13.0",
3232
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
3333
"@types/jest": "^29.5.3",
3434
"@typescript-eslint/eslint-plugin": "^6.20.0",
@@ -48,7 +48,7 @@
4848
},
4949
"peerDependencies": {
5050
"@langchain/core": "^0.2.0 || ^0.3.0",
51-
"@launchdarkly/server-sdk-ai": "^0.12.2",
51+
"@launchdarkly/server-sdk-ai": "^0.13.0",
5252
"langchain": "^0.2.0 || ^0.3.0"
5353
}
5454
}

packages/ai-providers/server-ai-langchain/src/LangChainProvider.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
LDLogger,
1111
LDMessage,
1212
LDTokenUsage,
13+
StructuredResponse,
1314
} from '@launchdarkly/server-sdk-ai';
1415

1516
/**
@@ -79,6 +80,38 @@ export class LangChainProvider extends AIProvider {
7980
};
8081
}
8182

83+
/**
84+
* Invoke the LangChain model with structured output support.
85+
*/
86+
async invokeStructuredModel(
87+
messages: LDMessage[],
88+
responseStructure: Record<string, unknown>,
89+
): Promise<StructuredResponse> {
90+
// Convert LDMessage[] to LangChain messages
91+
const langchainMessages = LangChainProvider.convertMessagesToLangChain(messages);
92+
93+
// Get the LangChain response
94+
const response = await this._llm
95+
.withStructuredOutput(responseStructure)
96+
.invoke(langchainMessages);
97+
98+
// Using structured output doesn't support metrics
99+
const metrics = {
100+
success: true,
101+
usage: {
102+
total: 0,
103+
input: 0,
104+
output: 0,
105+
},
106+
};
107+
108+
return {
109+
data: response,
110+
rawResponse: JSON.stringify(response),
111+
metrics,
112+
};
113+
}
114+
82115
/**
83116
* Get the underlying LangChain model instance.
84117
*/

0 commit comments

Comments
 (0)