googleapis/mcp-toolbox · error
failed to marshal job to JSON: %w
Error message
failed to marshal job to JSON: %w
What it means
After fetching the job proto, GetJob serializes it to JSON with protojson.Marshal. This error means protojson failed to marshal the *dataprocpb.Job. This is rare and usually indicates an invalid or corrupt proto message (e.g. a proto with invalid field state produced by custom middleware or an incompatible library version).
Source
Thrown at internal/sources/dataproc/dataproc.go:405
// GetJob gets a single job.
func (s *Source) GetJob(ctx context.Context, jobId string) (any, error) {
client := s.GetJobControllerClient()
req := &dataprocpb.GetJobRequest{
ProjectId: s.Project,
Region: s.Region,
JobId: jobId,
}
jobPb, err := client.GetJob(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to get job: %w", err)
}
jsonBytes, err := protojson.Marshal(jobPb)
if err != nil {
return nil, fmt.Errorf("failed to marshal job to JSON: %w", err)
}
var result map[string]any
if err := json.Unmarshal(jsonBytes, &result); err != nil {
return nil, fmt.Errorf("failed to unmarshal job JSON: %w", err)
}
consoleUrl := JobConsoleURLFromProto(jobPb, s.Region)
logsUrl, err := JobLogsURLFromProto(jobPb, s.Region)
if err != nil {
return nil, fmt.Errorf("error generating logs url: %v", err)
}
wrappedResult := map[string]any{
"consoleUrl": consoleUrl,
"logsUrl": logsUrl,
"job": result,
}View on GitHub (pinned to 8cc6e09de2)
Solutions
- Check go.mod for mismatched google.golang.org/protobuf versions and run go mod tidy
- Regenerate vendored dataprocpb code if protos were customized
- Log the error and retry GetJob; the failure is usually not deterministic in application logic
Example fix
// before require google.golang.org/protobuf v1.30.0 // after require google.golang.org/protobuf v1.33.0 // align with generated code, then: go mod tidy
Defensive patterns
Strategy: retry
Try / catch
job, err := svc.GetJob(ctx, jobId)
if err != nil && strings.Contains(err.Error(), "marshal job to JSON") {
// retry once; if persistent, align protobuf library versions
} Prevention
- Keep google.golang.org/protobuf version aligned with generated pb code
- Run go mod tidy after dependency upgrades
- Avoid patching generated proto types
When it happens
Trigger: protojson.Marshal returning an error on the fetched Job proto — practically only when the proto is in an invalid state (e.g. invalid oneof contents or incompatible google.golang.org/protobuf versions).
Common situations: Version skew between google.golang.org/protobuf and generated pb code; patched/instrumented protos; extremely rare in normal use.
Understand the failure class
Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.
Related errors
- failed to unmarshal job JSON: %w
- operation finished with error but could not marshal error ob
- failed to marshal operation to JSON: %w
- failed to create dataproc client: %w
- failed to create longrunning client: %w
AI-assisted analysis of googleapis/mcp-toolbox@8cc6e09de2 (2026-09-05).
Data as JSON: /api/errors/d0c64cc48b2e12e0.
Report an issue: GitHub.