@@ -495,27 +495,10 @@ func (cm *responsesAPIChatModel) genRequestAndOptions(in []*schema.Message, opti
495495 return nil , err
496496 }
497497
498- if err = cm .populateTools (reqParams .req , options .Tools ); err != nil {
498+ if err = cm .populateTools (reqParams .req , options .Tools , options . ToolChoice ); err != nil {
499499 return nil , err
500500 }
501501
502- if options .ToolChoice != nil {
503- var tco responses.ToolChoiceOptions
504- switch * options .ToolChoice {
505- case schema .ToolChoiceForbidden :
506- tco = responses .ToolChoiceOptionsNone
507- case schema .ToolChoiceAllowed :
508- tco = responses .ToolChoiceOptionsAuto
509- case schema .ToolChoiceForced :
510- tco = responses .ToolChoiceOptionsRequired
511- default :
512- tco = responses .ToolChoiceOptionsAuto
513- }
514- reqParams .req .ToolChoice = responses.ResponseNewParamsToolChoiceUnion {
515- OfToolChoiceMode : param .NewOpt (tco ),
516- }
517- }
518-
519502 for k , v := range specOptions .customHeaders {
520503 reqParams .opts = append (reqParams .opts , option .WithHeaderAdd (k , v ))
521504 }
@@ -840,7 +823,7 @@ func (cm *responsesAPIChatModel) toOpenaiMultiModalContent(msg *schema.Message)
840823 return content , nil
841824}
842825
843- func (cm * responsesAPIChatModel ) populateTools (req * responses.ResponseNewParams , optTools []* schema.ToolInfo ) error {
826+ func (cm * responsesAPIChatModel ) populateTools (req * responses.ResponseNewParams , optTools []* schema.ToolInfo , toolChoice * schema. ToolChoice ) error {
844827 // When caching is enabled, the tool is only passed on the first request.
845828 if req .PreviousResponseID .Valid () {
846829 return nil
@@ -857,6 +840,23 @@ func (cm *responsesAPIChatModel) populateTools(req *responses.ResponseNewParams,
857840
858841 req .Tools = tools
859842
843+ if toolChoice != nil {
844+ var tco responses.ToolChoiceOptions
845+ switch * toolChoice {
846+ case schema .ToolChoiceForbidden :
847+ tco = responses .ToolChoiceOptionsNone
848+ case schema .ToolChoiceAllowed :
849+ tco = responses .ToolChoiceOptionsAuto
850+ case schema .ToolChoiceForced :
851+ tco = responses .ToolChoiceOptionsRequired
852+ default :
853+ tco = responses .ToolChoiceOptionsAuto
854+ }
855+ req .ToolChoice = responses.ResponseNewParamsToolChoiceUnion {
856+ OfToolChoiceMode : param .NewOpt (tco ),
857+ }
858+ }
859+
860860 return nil
861861}
862862
0 commit comments