dagger/dagger · error

failed to encode response %T: %w

Error message

failed to encode response %T: %w

What it means

toolStructuredResponse: json.Encoder failed to serialize the tool's structured response value — the response contains a value JSON cannot encode (NaN, Inf, func, channel, cycle).

Source

Thrown at core/mcp.go:2667

}

// WorkspaceID returns the call.ID of the bound workspace, or nil if the LLM is
// not bound to a workspace. Used by step() to detect (and persist) an in-step
// workspace change, e.g. a Changeset overlaid by a tool.
func (m *MCP) WorkspaceID() (*call.ID, error) {
	if m.workspace.Self() == nil {
		return nil, nil
	}
	return m.workspace.ID()
}

func toolStructuredResponse(val any) (string, error) {
	str := new(strings.Builder)
	enc := json.NewEncoder(str)
	enc.SetEscapeHTML(false)
	enc.SetIndent("", "  ")
	if err := enc.Encode(val); err != nil {
		return "", fmt.Errorf("failed to encode response %T: %w", val, err)
	}
	return str.String(), nil
}

func limitLines(spanID string, logs []string, limit, maxLineLen int) []string {
	if limit > 0 && len(logs) > limit {
		snipped := fmt.Sprintf("... %d lines omitted (use ReadLogs(span: %s) to read more) ...", len(logs)-limit, spanID)
		logs = append([]string{snipped}, logs[len(logs)-limit:]...)
	}
	for i, line := range logs {
		if len(line) > maxLineLen {
			logs[i] = line[:maxLineLen] + fmt.Sprintf("[... %d chars truncated]", len(line)-maxLineLen)
		}
	}
	return logs
}

// limitIndirectLines abridges a captured log stream for a tool result: lines

View on GitHub (pinned to 82ba2681db)

Solutions

  1. Inspect the wrapped error for the unencodable field
  2. Sanitize numeric values (NaN/Inf) and drop non-serializable fields before returning
  3. Return a simpler response shape from the tool
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at core/mcp.go:2667 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of dagger/dagger@82ba2681db (2026-09-05). Data as JSON: /api/errors/e5f4e06a3c6bac62. Report an issue: GitHub.