Skip to content

Commit 9ddec6a

Browse files
committed
fix(go/ai): make supportsConstrained function work with Model interface
Refactor supportsConstrained to accept Model interface instead of *model type, enabling constrained output support for custom model implementations.
1 parent f88c7bc commit 9ddec6a

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

go/ai/generate.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func GenerateWithRequest(ctx context.Context, r api.Registry, opts *GenerateActi
282282
// Native constrained output is enabled only when the user has
283283
// requested it, the model supports it, and there's a JSON schema.
284284
outputCfg.Constrained = opts.Output.JsonSchema != nil &&
285-
opts.Output.Constrained && m != nil && m.(*model).supportsConstrained(len(toolDefs) > 0)
285+
opts.Output.Constrained && m != nil && supportsConstrained(m, len(toolDefs) > 0)
286286

287287
// Add schema instructions to prompt when not using native constraints.
288288
// This is a no-op for unstructured output requests.
@@ -560,12 +560,18 @@ func (m *model) Generate(ctx context.Context, req *ModelRequest, cb ModelStreamC
560560
}
561561

562562
// supportsConstrained returns whether the model supports constrained output.
563-
func (m *model) supportsConstrained(hasTools bool) bool {
563+
func supportsConstrained(m Model, hasTools bool) bool {
564+
var (
565+
metadata map[string]any
566+
)
564567
if m == nil {
565568
return false
566569
}
567570

568-
metadata := m.ActionDef.Desc().Metadata
571+
if act, ok := m.(api.Action); ok {
572+
metadata = act.Desc().Metadata
573+
}
574+
569575
if metadata == nil {
570576
return false
571577
}

0 commit comments

Comments
 (0)