{"record":{"id":"c3a9f66f8ae053f7","repo":"pola-rs/polars","slug":"cannot-infer-dtype-from-original-value-r-string","errorCode":null,"errorMessage":"cannot infer dtype from {original_value!r} string value","messagePattern":"cannot infer dtype from (.+?) string value","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/io/database/_inference.py","lineNumber":204,"sourceCode":"    # temporal dtypes\n    elif value.startswith((\"DATETIME\", \"TIMESTAMP\")) and not (value.endswith(\"[D]\")):\n        if any((tz in value.replace(\" \", \"\")) for tz in (\"TZ\", \"TIMEZONE\")):\n            if \"WITHOUT\" not in value:\n                return None  # there's a timezone, but we don't know what it is\n        unit = timeunit_from_precision(modifier) if modifier else \"us\"\n        dtype = Datetime(time_unit=(unit or \"us\"))  # type: ignore[arg-type]\n    else:\n        value = re.sub(r\"\\d\", \"\", value)\n        if value in (\"INTERVAL\", \"TIMEDELTA\", \"DURATION\"):\n            dtype = Duration\n        elif value == \"DATE\":\n            dtype = Date\n        elif value == \"TIME\":\n            dtype = Time\n\n    if not dtype and raise_unmatched:\n        msg = f\"cannot infer dtype from {original_value!r} string value\"\n        raise ValueError(msg)\n\n    return dtype\n\n\ndef dtype_from_cursor_description(\n    description: tuple[Any, ...],\n) -> PolarsDataType | None:\n    \"\"\"Attempt to infer Polars dtype from database cursor description `type_code`.\"\"\"\n    type_code, _disp_size, internal_size, precision, scale, *_ = description\n    dtype: PolarsDataType | None = None\n\n    if isclass(type_code):\n        # python types, eg: int, float, str, etc\n        with suppress(TypeError):\n            dtype = parse_py_type_into_dtype(type_code)  # type: ignore[arg-type]\n\n    elif isinstance(type_code, str):\n        # database/sql type names, eg: \"VARCHAR\", \"NUMERIC\", \"BLOB\", etc","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/pola-rs/polars/blob/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/io/database/_inference.py#L186-L222","documentation":"Raised by polars.io.database._inference when schema inference cannot map a string cell value to any known dtype and raise_unmatched is set. During read_database row-level fallback, object/string values are pattern-matched against recognised formats (numbers, dates, times, intervals, booleans); an unrecognisable value aborts inference rather than guessing.","triggerScenarios":"read_database() returning row-level data where a column contains string values that match no supported pattern (custom serialisations, UUIDs with odd formatting, driver-specific type names, exotic interval literals); raise_unmatched=True is set when the value would otherwise silently become String and the caller asked for strict inference.","commonSituations":"Reading from databases whose drivers stringify unusual types (Oracle intervals, MSSQL sql_variant, PG enums/circles); schema_overrides not supplied for columns holding non-standard textual data; data with mixed/malformed values in a column expected to be typed.","solutions":["Pass schema_overrides={'col': pl.String} (or the correct dtype) for the offending column so inference is bypassed","Load the column as String and parse/cast afterwards with str.strptime / str.to_* and errors-facing logic","Clean or normalise the source values, or select CAST(... AS VARCHAR) on the database side to make the type explicit"],"exampleFix":"# before\ndf = pl.read_database(\"SELECT metadata_col, val FROM t\", conn)  # inference fails on odd string\n\n# after\ndf = pl.read_database(\n    \"SELECT metadata_col, val FROM t\", conn,\n    schema_overrides={\"metadata_col\": pl.String},\n)","handlingStrategy":"validation","validationCode":"import polars as pl\n\n# force string loading for free-form text columns, bypassing inference\nOVERRIDE = {c: pl.String for c in likely_untyped_columns}\ndf = pl.read_database(query, conn, schema_overrides=OVERRIDE)","typeGuard":null,"tryCatchPattern":"try:\n    df = pl.read_database(query, conn)\nexcept ValueError as e:\n    if \"cannot infer dtype\" in str(e):\n        df = pl.read_database(query, conn, schema_overrides={col: pl.String for col in all_cols})\n    else:\n        raise","preventionTips":["Always supply schema_overrides for columns with driver-specific or custom-serialised values","Load unknown columns as pl.String first, then parse with strptime/to_integer in polars","Cast exotic types to VARCHAR in the SQL itself so types are explicit"],"tags":["database","schema-inference","dtype","value-error"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}