xtekky/gpt4free · error · ValueError

Unexpected token {kind!r}={value!r} in condition expression

Error message

Unexpected token {kind!r}={value!r} in condition expression

What it means

ValueError from _parse_atom in config_provider: the tokenizer produced a token whose kind is not one of float/int/id at a position where an operand was expected. This means the condition string contains characters the lexer classified as something else (an operator or stray symbol) where a value had to appear.

Source

Thrown at g4f/providers/config_provider.py:298

        if root not in variables:
            raise ValueError(f"Unknown variable in condition: {root!r}")

        result = variables[root]
        for part in parts[1:]:
            if isinstance(result, dict):
                result = result.get(part)
                if result is None:
                    result = 0.0
                    break
            else:
                raise ValueError(
                    f"Cannot access field {part!r} on non-dict value "
                    f"while resolving {value!r}"
                )

        return float(result) if result is not None else 0.0, pos
    else:
        raise ValueError(f"Unexpected token {kind!r}={value!r} in condition expression")


def evaluate_condition(
    condition: str,
    quota: Optional[Dict],
    error_count: int,
) -> bool:
    """Evaluate a provider condition string.

    The condition may reference:

    * ``quota``              – the full quota dict returned by ``get_quota()``.
      Each provider returns its own schema.  Access nested fields with
      dot-notation, e.g. ``quota.balance``, ``quota.credits.remaining``.
      Missing keys resolve to ``0.0``.
    * ``balance``            – shorthand alias for ``quota.balance``.
      Kept for backward compatibility; equivalent to ``quota.balance``
      for providers that return ``{"balance": float}`` (e.g. PollinationsAI).

View on GitHub (pinned to 973504e177)

Solutions

  1. Simplify the condition to what the grammar supports: single comparisons between dotted variables and numeric literals.
  2. Remove boolean connectors — combine multiple conditions as separate provider entries or rely on default eligibility.
  3. Re-test the edited condition via evaluate_condition('<cond>', {'quota': {...}}, 0).
  4. Quarantine the broken entry (comment it out) to unblock the rest of config.yaml while you fix it.

Example fix

# before (config.yaml)
condition: "quota.balance > 0 and error_count < 3"

# after
condition: "quota.balance > 0"
Defensive patterns

Strategy: validation

Validate before calling

import re
OK = re.compile(r'^[A-Za-z_][\w.]*(==|!=|>=|<=|>|<)\s*(\d+(\.\d+)?)$')
bad = [c for c in my_conditions if not OK.match(c.strip())]
if bad:
    raise ValueError(f'unsupported condition syntax: {bad}')

Prevention

When it happens

Trigger: Conditions with misplaced operators or unsupported syntax reaching an operand slot, e.g. 'quota.balance > > 0', '!!quota', or strings/operators in positions the tiny parser does not support (no parentheses, no and/or in this grammar).

Common situations: Writing Python- or SQL-style boolean logic ('quota.a > 0 and quota.b > 0') that this mini-grammar cannot parse; stray punctuation from YAML copy-paste; unquoted special characters being interpreted oddly.

Understand the failure class

Related errors


AI-assisted analysis of xtekky/gpt4free@973504e177 (2026-08-14). Data as JSON: /api/errors/0cc0c5f660da76e3. Report an issue: GitHub.