pola-rs/polars · error · ValueError

invalid Config string (did you mean to use…

Error message

invalid Config string (did you mean to use `load_from_file`?)

What it means

Raised by Config.load when the given string is not valid JSON. Since load expects the JSON string produced by Config.save(), a JSONDecodeError usually means the user passed a file path instead of file contents.

Solutions

  1. Use Config.load_from_file(path) when you have a file path
  2. Pass the exact JSON string returned by Config.save()
  3. Validate the string parses as JSON before calling load

Example fix

// before
pl.Config.load('~/.polars_cfg.json')  # ValueError
// after
pl.Config.load_from_file('~/.polars_cfg.json')
Defensive patterns

Strategy: try-catch

Validate before calling

import json
try:
    json.loads(cfg)
except json.JSONDecodeError:
    raise ValueError('config string is not JSON; use load_from_file for paths')

Type guard

def is_json(s: str) -> bool:
    try:
        json.loads(s)
        return True
    except json.JSONDecodeError:
        return False

Try / catch

try:
    pl.Config.load(cfg)
except ValueError:
    # maybe a path was passed
    pl.Config.load_from_file(cfg)

Prevention

When it happens

Trigger: Calling Config.load('/path/to/config.json') (a path, not a JSON string) or passing any non-JSON string.

Common situations: Confusing Config.load (string) with Config.load_from_file (path); passing a dict/serialized YAML instead of the polars JSON format.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/c95b9425b4f451df. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/config.py:408

    def load(cls, cfg: str) -> Config:
        """
        Load (and set) previously saved Config options from a JSON string.

        Parameters
        ----------
        cfg : str
            JSON string produced by `Config.save()`.

        See Also
        --------
        load_from_file : Load (and set) Config options from a JSON file.
        save : Save the current set of Config options as a JSON string or file.
        """
        try:
            options = json.loads(cfg)
        except json.JSONDecodeError as err:
            msg = "invalid Config string (did you mean to use `load_from_file`?)"
            raise ValueError(msg) from err

        cfg_load = Config()
        opts = options.get("environment", {})
        if "POLARS_ENGINE_AFFINITY" in opts:
            # A saved affinity value replaces any object affinity.
            set_engine_affinity_override(None)
        for key, opt in opts.items():
            if opt is None:
                os.environ.pop(key, None)
            else:
                os.environ[key] = opt

        for cfg_methodname, value in options.get("direct", {}).items():
            if hasattr(cfg_load, cfg_methodname):
                getattr(cfg_load, cfg_methodname)(value)

        plr.config_reload_env_vars()
        return cfg_load

View on GitHub (pinned to fe841f959e)