@@ -4,7 +4,14 @@ import {
4
4
type BaseChatModelParams ,
5
5
} from "@langchain/core/language_models/chat_models" ;
6
6
import { getEnvironmentVariable } from "@langchain/core/utils/env" ;
7
- import { ChatOpenAI } from "../chat_models.js" ;
7
+ import { BaseLanguageModelInput } from "@langchain/core/language_models/base" ;
8
+ import { BaseMessage } from "@langchain/core/messages" ;
9
+ import { Runnable } from "@langchain/core/runnables" ;
10
+ import { z } from "zod" ;
11
+ import {
12
+ ChatOpenAI ,
13
+ ChatOpenAIStructuredOutputMethodOptions ,
14
+ } from "../chat_models.js" ;
8
15
import { OpenAIEndpointConfig , getEndpoint } from "../utils/azure.js" ;
9
16
import {
10
17
AzureOpenAIInput ,
@@ -635,4 +642,64 @@ export class AzureChatOpenAI extends ChatOpenAI {
635
642
636
643
return json ;
637
644
}
645
+
646
+ withStructuredOutput <
647
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
648
+ RunOutput extends Record < string , any > = Record < string , any >
649
+ > (
650
+ outputSchema :
651
+ | z . ZodType < RunOutput >
652
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
653
+ | Record < string , any > ,
654
+ config ?: ChatOpenAIStructuredOutputMethodOptions < false >
655
+ ) : Runnable < BaseLanguageModelInput , RunOutput > ;
656
+
657
+ withStructuredOutput <
658
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
659
+ RunOutput extends Record < string , any > = Record < string , any >
660
+ > (
661
+ outputSchema :
662
+ | z . ZodType < RunOutput >
663
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
664
+ | Record < string , any > ,
665
+ config ?: ChatOpenAIStructuredOutputMethodOptions < true >
666
+ ) : Runnable < BaseLanguageModelInput , { raw : BaseMessage ; parsed : RunOutput } > ;
667
+
668
+ withStructuredOutput <
669
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
670
+ RunOutput extends Record < string , any > = Record < string , any >
671
+ > (
672
+ outputSchema :
673
+ | z . ZodType < RunOutput >
674
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
675
+ | Record < string , any > ,
676
+ config ?: ChatOpenAIStructuredOutputMethodOptions < boolean >
677
+ ) :
678
+ | Runnable < BaseLanguageModelInput , RunOutput >
679
+ | Runnable < BaseLanguageModelInput , { raw : BaseMessage ; parsed : RunOutput } > ;
680
+
681
+ withStructuredOutput <
682
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
683
+ RunOutput extends Record < string , any > = Record < string , any >
684
+ > (
685
+ outputSchema :
686
+ | z . ZodType < RunOutput >
687
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
688
+ | Record < string , any > ,
689
+ config ?: ChatOpenAIStructuredOutputMethodOptions < boolean >
690
+ ) :
691
+ | Runnable < BaseLanguageModelInput , RunOutput >
692
+ | Runnable <
693
+ BaseLanguageModelInput ,
694
+ { raw : BaseMessage ; parsed : RunOutput }
695
+ > {
696
+ const ensuredConfig = { ...config } ;
697
+ // Not all Azure gpt-4o deployments models support jsonSchema yet
698
+ if ( this . model . startsWith ( "gpt-4o" ) ) {
699
+ if ( ensuredConfig ?. method === undefined ) {
700
+ ensuredConfig . method = "functionCalling" ;
701
+ }
702
+ }
703
+ return super . withStructuredOutput < RunOutput > ( outputSchema , ensuredConfig ) ;
704
+ }
638
705
}
0 commit comments