dgraph-io/dgraph · error

While parsing namespace:

Error message

While parsing namespace:

What it means

parse wraps any error from parseNamespace with 'While parsing namespace:'. This is an outer context message: the actual failure is one of the inner namespace errors (peek failure, type mismatch, or no schema after namespace). It indicates the schema string contains a '[' section that could not be parsed as a namespace.

Source

Thrown at schema/parse.go:691

			}
			return &result, nil

		case itemText:
			// For schema which does not contain the namespace information, use the default
			// namespace, if namespace has to be preserved. Else, use the passed namespace.
			ns := x.RootNamespace
			if namespace != math.MaxUint64 {
				ns = namespace
			}
			if err := parseTypeOrSchema(item, it, ns); err != nil {
				return nil, err
			}

		case itemLeftSquare:
			// We expect a namespace.
			ns, err := parseNamespace(it)
			if err != nil {
				return nil, errors.Wrapf(err, "While parsing namespace:")
			}
			if namespace != math.MaxUint64 {
				// Use the passed namespace, if we don't want to preserve the namespace.
				ns = namespace
			}
			// We have already called next in parseNamespace.
			item := it.Item()
			if err := parseTypeOrSchema(item, it, ns); err != nil {
				return nil, err
			}

		case itemNewLine:
			// pass empty line

		default:
			return nil, it.Item().Errorf("Unexpected token: %v while parsing schema", item)
		}
	}

View on GitHub (pinned to 759e242be6)

Solutions

  1. Inspect the wrapped inner error to see the specific token problem
  2. Fix the namespace section to the form [0x1] (number then closing bracket), or remove it if namespaces are unnecessary
  3. If interpolating namespace values in scripts, validate the value is a non-empty number before templating
  4. Log the full schema string at parse time to locate the offending bracket

Example fix

// before
schema := fmt.Sprintf("[%s]\nname: string .", ns) // ns empty -> "[]"
// after
schema := fmt.Sprintf("[0x%x]\nname: string .", nsUint)
Defensive patterns

Strategy: validation

Validate before calling

if strings.Contains(schemaStr, "[") && !regexp.MustCompile(`\[0x[0-9a-fA-F]+\]`).MatchString(schemaStr) {
    return fmt.Errorf("malformed namespace bracket in schema")
}

Try / catch

if _, err := schema.Parse(s, 0, 1); err != nil {
    if strings.Contains(err.Error(), "While parsing namespace") {
        return fmt.Errorf("fix [ns] section: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: schema.Parse/ParseWithNamespace/ToDirectedEdges given schema text with malformed namespace brackets such as '[' with no number, '[x]', or '[' at EOF.

Common situations: Manual schema edits breaking the [0x1] header; scripts interpolating an empty namespace variable producing '[]' or '['; concatenating schema files incorrectly.

Related errors


AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01). Data as JSON: /api/errors/0e0e419529ef358f. Report an issue: GitHub.