langchain-ai/deepagents · error · ValueError

Unbalanced braces in --model-params value

Error message

Unbalanced braces in --model-params value

What it means

When the --model-params value starts with '{', _extract_model_params_flag tracks brace depth to find the matching closing '}'. If the end of the argument is reached before depth returns to 0, it raises ValueError because the JSON object braces are unbalanced. A well-formed object must have equal opening and closing braces.

Source

Thrown at libs/code/deepagents_code/app.py:1969

            raise ValueError(msg)
        # Parse the quoted token with shlex so escaped quotes are unescaped.
        json_str = shlex.split(after[: end + 1], posix=True)[0]
        rest = after[end + 1 :].lstrip()
    elif after[0] == "{":
        # Walk forward to find the matching closing brace.
        depth = 0
        end = -1
        for i, ch in enumerate(after):
            if ch == "{":
                depth += 1
            elif ch == "}":
                depth -= 1
                if depth == 0:
                    end = i
                    break
        if end == -1:
            msg = "Unbalanced braces in --model-params value"
            raise ValueError(msg)
        json_str = after[: end + 1]
        rest = after[end + 1 :].lstrip()
    else:
        # Non-brace, non-quoted — take the next whitespace-delimited token.
        parts = after.split(None, 1)
        json_str = parts[0]
        rest = parts[1] if len(parts) > 1 else ""

    remaining = f"{before} {rest}".strip()
    try:
        params = json.loads(json_str)
    except json.JSONDecodeError:
        msg = (
            f"Invalid JSON in --model-params: {json_str!r}. "
            'Expected format: --model-params \'{"key": "value"}\''
        )
        raise ValueError(msg) from None
    if not isinstance(params, dict):

View on GitHub (pinned to a1af029e6e)

Solutions

  1. Balance the braces: --model-params '{"temperature":0.2}'.
  2. Validate the JSON locally first, e.g. python -c 'import json,sys; json.loads(sys.argv[1])' '{...}', before passing it to the CLI.
  3. Single-quote the whole value to prevent shell interpretation, and check nested objects have their own closing braces.

Example fix

# before
dcode --model-params '{"temperature":0.2

# after
dcode --model-params '{"temperature":0.2}'
Defensive patterns

Strategy: try-catch

Validate before calling

value = raw.split(None, 1)[1].strip() if len(raw.split(None, 1)) > 1 else ''
if value.startswith('{') and value.count('{') != value.count('}'):
    import json
    try:
        json.loads(value)
    except json.JSONDecodeError as e:
        raise SystemExit(f"unbalanced braces in --model-params: {e}")

Try / catch

try:
    handle_command(argv)
except ValueError as e:
    if "Unbalanced braces" in str(e):
        print("ensure the --model-params JSON object has matching braces: --model-params '{...}'")
        sys.exit(2)
    raise

Prevention

When it happens

Trigger: Passing --model-params '{"temperature":0.2' (missing the closing brace) or a value where extra '{' without matching '}' leaves depth > 0 at end of input.

Common situations: Truncated paste of a multi-key JSON object; shell brace-expansion or history expansion eating characters; generating the flag from a template where the closing brace was on a dropped line; nested objects where an inner brace was forgotten.

Related errors


AI-assisted analysis of langchain-ai/deepagents@a1af029e6e (2026-08-29). Data as JSON: /api/errors/6ecce68f464fc9d4. Report an issue: GitHub.