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
- Close the IRI with `>` before the offending character
- Escape or remove illegal characters inside IRIs per the IRIREF grammar
- Find and fix the producer emitting malformed IRIs
- 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
- Sanitize generated IRIs to the IRIREF character set
- Reject unescaped < > and control characters inside IRIs
- Test serialization output of upstream RDF producers
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
- Unexpected end of IRI
- NQuad failed sanity check. Subject: %q, Predicate: %q, Objec
- expected '(', found: %s
- expected variable name, found: %s
- empty variable name in function call
AI-assisted analysis of dgraph-io/dgraph@759e242be6 (2026-09-01).
Data as JSON: /api/errors/5ecf44d70bb8e939.
Report an issue: GitHub.