googleapis/mcp-toolbox · error

failed to unmarshal operation bytes: %w

Error message

failed to unmarshal operation bytes: %w

What it means

After successfully marshaling the completed operation to JSON bytes, GetOperations unmarshals those bytes into a generic any so the result can be returned as structured data. This error means the round-trip failed — the JSON bytes the tool itself just produced could not be parsed, which indicates a deep inconsistency (corrupt bytes, encoding issue) in the marshaled payload.

Source

Thrown at internal/sources/alloydbadmin/alloydbadmin.go:371

			var opBytes []byte
			opBytes, err = op.MarshalJSON()
			if err != nil {
				return nil, fmt.Errorf("could not marshal operation: %w", err)
			}

			if op.Response != nil {
				var responseData map[string]any
				if err := json.Unmarshal(op.Response, &responseData); err == nil && responseData != nil {
					if msg, ok := generateAlloyDBConnectionMessage(responseData, connectionMessageTemplate); ok {
						return msg, nil
					}
				}
			}

			var result any
			if err := json.Unmarshal(opBytes, &result); err != nil {
				return nil, fmt.Errorf("failed to unmarshal operation bytes: %w", err)
			}
			return result, nil
		}
		logger.DebugContext(ctx, fmt.Sprintf("Operation not complete, retrying in %v\n", delay))
	}
	return nil, nil
}

func generateAlloyDBConnectionMessage(responseData map[string]any, connectionMessageTemplate string) (string, bool) {
	resourceName, ok := responseData["name"].(string)
	if !ok {
		return "", false
	}

	parts := strings.Split(resourceName, "/")
	var project, region, cluster, instance string

	// Expected format: projects/{project}/locations/{location}/clusters/{cluster}

View on GitHub (pinned to 8cc6e09de2)

Solutions

  1. Retry the call; inspect the wrapped error for 'invalid character' or 'invalid UTF-8' clues.
  2. Log/dump opBytes (or a safe prefix) to see what failed to parse.
  3. Check the operation payload via `gcloud alloydb operations describe <op>` for non-UTF-8 or unusual metadata.
  4. Upgrade the AlloyDB Admin Go SDK to the latest version.
  5. If reproducible, file a bug against the SDK's operation MarshalJSON.
Defensive patterns

Strategy: retry

Validate before calling

// Ensure the operation payload is sane via a direct API read before round-tripping
op, err := adminClient.GetOperation(ctx, &adminpb.GetOperationRequest{Name: opName})
if err != nil { return err }
if _, err := json.Marshal(op); err != nil { return fmt.Errorf("operation payload not serializable: %w", err) }

Try / catch

result, err := toolbox.GetOperations(ctx, opName)
var opErr *errLookup
if errors.As(err, &opErr) && strings.Contains(err.Error(), "failed to unmarshal operation bytes") {
    // fall back to describing the operation directly via the Admin API
}

Prevention

When it happens

Trigger: Calling GetOperations on a DONE operation where json.Unmarshal(opBytes, &result) fails — practically only when MarshalJSON produced bytes that are not valid JSON (e.g. embedded invalid UTF-8 or non-standard JSON from the operation's MarshalJSON implementation).

Common situations: Operation payloads containing invalid UTF-8 strings or exotic values that marshal to non-standard JSON; SDK version bugs in the longrunning operation's MarshalJSON.

Understand the failure class

Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.

Related errors


AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05). Data as JSON: /api/errors/c64551e3d6824ffc. Report an issue: GitHub.