micro/go-micro · error
ai model is nil
Error message
ai model is nil
What it means
GenerateWithRetry validates its Model argument up front and returns this error immediately if m is nil, before any retry loop runs. It prevents a nil-pointer panic inside the retry/timeout machinery and surfaces the misconfiguration clearly.
Source
Thrown at ai/retry.go:147
}
// GeneratePolicy controls timeout and retry behavior for a model call.
type GeneratePolicy struct {
Timeout time.Duration
MaxAttempts int
Backoff time.Duration
// Jitter adds up to this duration of random delay to retry backoff.
// It is opt-in so existing retry timing remains deterministic by default.
Jitter time.Duration
}
// GenerateWithRetry calls m.Generate with per-attempt timeout and bounded retry.
func GenerateWithRetry(ctx context.Context, m Model, req *Request, policy GeneratePolicy, opts ...GenerateOption) (*Response, error) {
if policy.MaxAttempts <= 0 {
policy.MaxAttempts = 1
}
if m == nil {
return nil, errors.New("ai model is nil")
}
var last error
for attempt := 1; attempt <= policy.MaxAttempts; attempt++ {
if err := ctx.Err(); err != nil {
return nil, err
}
callCtx := ctx
cancel := func() {}
if policy.Timeout > 0 {
callCtx, cancel = context.WithTimeout(ctx, policy.Timeout)
}
if info, ok := RunInfoFrom(callCtx); ok {
info.Attempt = attempt
info.MaxAttempts = policy.MaxAttempts
callCtx = WithRunInfo(callCtx, info)
}View on GitHub (pinned to 24529f1404)
Solutions
- Check the error from model/provider construction before calling GenerateWithRetry
- Verify the model configuration is loaded (provider name, API key, model id) so the factory returns a non-nil Model
- Add a nil check at the call site: if m == nil { return error before retrying }
Example fix
// before
model, _ := openai.New(cfg) // error ignored
resp, err := ai.GenerateWithRetry(ctx, model, req, policy)
// after
model, err := openai.New(cfg)
if err != nil {
return err
}
if model == nil {
return errors.New("model not configured")
}
resp, err := ai.GenerateWithRetry(ctx, model, req, policy) Defensive patterns
Strategy: validation
Validate before calling
if model == nil {
return nil, errors.New("ai model not configured")
}
resp, err := ai.GenerateWithRetry(ctx, model, req, policy) Type guard
func modelReady(m ai.Model) bool { return m != nil } Try / catch
resp, err := ai.GenerateWithRetry(ctx, model, req, policy)
if err != nil && strings.Contains(err.Error(), "ai model is nil") {
return fmt.Errorf("model misconfigured: construction failed or config missing")
} Prevention
- Always check errors from model/provider constructors before use
- Fail fast at startup if the configured model resolves to nil
- Keep model construction in one place so nil models can't leak into call sites
When it happens
Trigger: Calling ai.GenerateWithRetry with a nil Model — typically because model construction failed and the error was ignored, or a config lookup returned nil.
Common situations: Ignoring the error from provider/model factory functions; optional model config keys missing so the resolved model is nil; struct fields left zero because initialization is deferred or conditional.
Related errors
- flow: step %q has no Run function
- flow: step %d has an empty name
- agent: ResumeStreamAsk requires a checkpoint
- ErrStreamingUnsupported
- API error: nil response
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/682fbe2a990a1d3d.
Report an issue: GitHub.