xtekky/gpt4free · error · ValueError

Cannot access field {part!r} on non-dict value while resolvi

Error message

Cannot access field {part!r} on non-dict value while resolving {value!r}

What it means

ValueError raised while resolving a dotted variable in a config.yaml condition: the root resolved successfully, but an intermediate segment was not a dict, so result.get(part) is impossible. E.g. 'quota.balance.free' where quota.balance is already a number. The message names the offending field and the full dotted path.

Source

Thrown at g4f/providers/config_provider.py:291

        # Legacy alias: "get_quota.balance" → "quota.balance"
        if value == "get_quota.balance":
            value = "quota.balance"

        # Resolve dotted paths: "quota.credits.remaining", "balance", etc.
        parts = value.split(".")
        root = parts[0]
        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:

View on GitHub (pinned to 973504e177)

Solutions

  1. Inspect the provider's actual get_quota() output and flatten the condition to stop at the scalar level ('quota.balance > 0').
  2. If multiple providers share one config, give each provider its own correctly-shaped condition instead of one generic condition.
  3. Test with evaluate_condition(condition, await provider.get_quota(), 0) before deploying config.yaml.
  4. Update g4f — quota schemas are occasionally normalized across providers.

Example fix

# before (config.yaml)
condition: "quota.balance.remaining > 0"

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

Strategy: validation

Validate before calling

quota = await provider.get_quota() if hasattr(provider, 'get_quota') else None
if quota is not None:
    evaluate_condition(condition, quota, 0)  # raises here, not mid-request, if paths mismatch

Type guard

def path_is_numeric(quota: dict, dotted: str) -> bool:
    node = quota
    for part in dotted.split('.'):
        if not isinstance(node, dict) or part not in node:
            return False
        node = node[part]
    return isinstance(node, (int, float))

Prevention

When it happens

Trigger: A condition walks deeper than the data shape: 'quota.balance.remaining > 0' when quota.balance is a float, or 'quota.x.y' where quota.x is a list/string.

Common situations: Providers whose get_quota() returns flat numbers where the condition expects nested dicts; conditions written against a different provider's quota schema; schema drift after a provider update changes nesting.

Related errors


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