vxcontrol/pentagi · error

failed to unmarshal primary agent msg chain %d: %w

Error message

failed to unmarshal primary agent msg chain %d: %w

What it means

PerformAgentChain wraps a json.Unmarshal failure when decoding msgChain.Chain (a []byte JSON column) into []llms.MessageContent. The stored chain JSON does not match the langchaingo llms.MessageContent schema expected by this build.

Source

Thrown at backend/pkg/providers/assistant.go:159

		"msg_chain_id": ap.msgChainID,
	})

	useAgents, err := ap.getAssistantUseAgents(ctx)
	if err != nil {
		logger.WithError(err).Error("failed to get assistant use agents")
		return fmt.Errorf("failed to get assistant use agents: %w", err)
	}

	msgChain, err := ap.fp.DB().GetMsgChain(ctx, ap.msgChainID)
	if err != nil {
		logger.WithError(err).Error("failed to get primary agent msg chain")
		return fmt.Errorf("failed to get primary agent msg chain %d: %w", ap.msgChainID, err)
	}

	var chain []llms.MessageContent
	if err := json.Unmarshal(msgChain.Chain, &chain); err != nil {
		logger.WithError(err).Error("failed to unmarshal primary agent msg chain")
		return fmt.Errorf("failed to unmarshal primary agent msg chain %d: %w", ap.msgChainID, err)
	}

	adviser, err := ap.fp.GetAskAdviceHandler(ctx, nil, nil)
	if err != nil {
		logger.WithError(err).Error("failed to get ask advice handler")
		return fmt.Errorf("failed to get ask advice handler: %w", err)
	}

	coder, err := ap.fp.GetCoderHandler(ctx, nil, nil)
	if err != nil {
		logger.WithError(err).Error("failed to get coder handler")
		return fmt.Errorf("failed to get coder handler: %w", err)
	}

	installer, err := ap.fp.GetInstallerHandler(ctx, nil, nil)
	if err != nil {
		logger.WithError(err).Error("failed to get installer handler")
		return fmt.Errorf("failed to get installer handler: %w", err)

View on GitHub (pinned to ea665308ba)

Solutions

  1. Inspect the msg_chains.Chain value for the logged id; fix or delete the corrupt row so a fresh chain is prepared.
  2. Check for a langchaingo/vxcontrol version bump in go.mod; roll back or migrate stored chains to the new schema.
  3. Re-run the flow: PrepareAgentChain will create a new valid chain.
  4. Add a schema-version column or migration for stored chains before upgrading.

Example fix

// before
var chain []llms.MessageContent
if err := json.Unmarshal(msgChain.Chain, &chain); err != nil {
    return fmt.Errorf("failed to unmarshal primary agent msg chain %d: %w", ap.msgChainID, err)
}
// after
var chain []llms.MessageContent
if err := json.Unmarshal(msgChain.Chain, &chain); err != nil {
    logger.WithError(err).Warn("corrupt chain, recreating")
    return ap.ResetAndPrepareChain(ctx) // rebuild chain instead of failing hard
}
Defensive patterns

Strategy: fallback

Validate before calling

if !json.Valid(msgChain.Chain) { return errors.New("stored chain is not valid JSON — recreate the chain") }

Type guard

func isValidChain(raw []byte) bool {
    var c []llms.MessageContent
    return json.Unmarshal(raw, &c) == nil
}

Try / catch

var chain []llms.MessageContent
if err := json.Unmarshal(msgChain.Chain, &chain); err != nil {
    logger.WithError(err).Warn("corrupt stored chain, rebuilding")
    return ap.PrepareAgentChain(ctx)
}

Prevention

When it happens

Trigger: Chain column contains corrupt/truncated JSON, was written by an older PentAGI version whose llms.MessageContent shape differs, or manual DB editing left invalid JSON.

Common situations: Upgrading PentAGI/langchaingo after the chain was serialized with a different struct layout; crash mid-write left partial JSON; operator edited the row by hand.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/b5303e78e6585c11. Report an issue: GitHub.