dgraph-io/dgraph · error

Unexpected character %q while parsing IRI

Error message

Unexpected character %q while parsing IRI

What it means

After reading an IRI's characters, IRIRef requires the terminator `>`. If the next rune is anything else (not EOF, not `>`), the lexer reports the unexpected character with %q formatting, indicating malformed IRI syntax in the input stream.

Source

Thrown at lex/iri.go:22

 */

package lex

import (
	"github.com/pkg/errors"
)

// IRIRef emits an IRIREF or returns an error if the input is invalid.
func IRIRef(l *Lexer, styp ItemType) error {
	l.Ignore() // ignore '<'
	l.AcceptRunRec(isIRIRefChar)
	l.Emit(styp) // will emit without '<' and '>'
	r := l.Next()
	if r == EOF {
		return errors.New("Unexpected end of IRI")
	}
	if r != '>' {
		return errors.Errorf("Unexpected character %q while parsing IRI", r)
	}
	l.Ignore() // ignore '>'
	return nil
}

// isIRIRefChar returns whether the rune is a character allowed in an IRIRef.
// IRIREF ::= '<' ([^#x00-#x20<>"{}|^`\] | UCHAR)* '>'
func isIRIRefChar(r rune, l *Lexer) bool {
	if r <= 32 { // no chars b/w 0x00 to 0x20 inclusive
		return false
	}
	switch r {
	case '<', '>', '"', '{', '}', '|', '^', '`':
		return false
	case '\\':
		r2 := l.Next()
		if r2 != 'u' && r2 != 'U' {
			l.Backup()

View on GitHub (pinned to 759e242be6)

Solutions

  1. Close the IRI with `>` before the offending character
  2. Escape or remove illegal characters inside IRIs per the IRIREF grammar
  3. Find and fix the producer emitting malformed IRIs
  4. Pre-validate lines of the RDF document with a regex like <[^<>()"]*>

Example fix

// before
<http://example.com/s> <http://example.com/p>.
// after
<http://example.com/s> <http://example.com/p> .
Defensive patterns

Strategy: validation

Validate before calling

iriRe := regexp.MustCompile(`^<[^<>"{}|^` + "`" + `\\\s]*>$`)
if !iriRe.MatchString(tok) {
  return fmt.Errorf("invalid IRI syntax: %s", tok)
}

Try / catch

if err := lex.IRIRef(l, item); err != nil {
  var synErr *SyntaxError
  if strings.Contains(err.Error(), "Unexpected character") {
    return fmt.Errorf("malformed IRI near offset %d: %w", l.Pos(), err)
  }
  return err
}

Prevention

When it happens

Trigger: Input like `<http://example.com/x http://...` where a space, newline, or other character appears where the closing `>` should be; nested or unescaped `<>` characters inside an IRI.

Common situations: Unescaped characters in generated IRIs, copy-pasted data with smart quotes or stray characters, IRIs split across lines without closing, or malformed N-Quads from a producer bug.

Related errors


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