{"record":{"id":"a7e597899f538ede","repo":"pola-rs/polars","slug":"pystring-is-not-valid-utf-8","errorCode":null,"errorMessage":"PyString is not valid UTF-8","messagePattern":"PyString is not valid UTF-8","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-python/src/file.rs","lineNumber":94,"sourceCode":"    }\n\n    pub(crate) fn to_buffer(&self) -> Buffer<u8> {\n        Python::attach(|py| {\n            let bytes = self\n                .inner\n                .call_method(py, \"read\", (), None)\n                .expect(\"no read method found\");\n\n            if let Ok(b) = bytes.cast_bound::<PyBytes>(py) {\n                // SAFETY: we keep the underlying python object alive.\n                let slice = b.as_bytes();\n                let owner = bytes.clone_ref(py);\n                let ss = unsafe { SharedStorage::from_slice_with_owner(slice, owner) };\n                return Buffer::from_storage(ss);\n            }\n\n            if let Ok(b) = bytes.cast_bound::<PyString>(py) {\n                return match b.to_cow().expect(\"PyString is not valid UTF-8\") {\n                    Cow::Borrowed(v) => {\n                        // SAFETY: we keep the underlying python object alive.\n                        let slice = v.as_bytes();\n                        let owner = bytes.clone_ref(py);\n                        let ss = unsafe { SharedStorage::from_slice_with_owner(slice, owner) };\n                        return Buffer::from_storage(ss);\n                    },\n                    Cow::Owned(v) => Buffer::from_vec(v.into_bytes()),\n                };\n            }\n\n            panic!(\"Expecting to be able to downcast into bytes from read result.\");\n        })\n    }\n\n    /// Validates that the underlying\n    /// python object has a `read`, `write`, and `seek` methods in respect to parameters.\n    /// Will return a `TypeError` if object does not have `read`, `seek`, and `write` methods.","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/pola-rs/polars/blob/5d8ebabf11caea54a5c29178a64a058762f49766/crates/polars-python/src/file.rs#L76-L112","documentation":"Internal expect() in PyFileLikeObject::to_buffer: the Python read() call returned a str, but converting it with to_cow() failed because the string's contents are not valid UTF-8, so it cannot become a byte buffer.","triggerScenarios":"This panic/expect fires when execution reaches an unguarded state described by: \"PyString is not valid UTF-8\". Typical triggers: unsupported dtype or feature-gated code path reached at runtime, invalid user input or environment variable value, calling API methods in the wrong order or on mismatched types, or data (lengths, offsets, ranges) violating the function's preconditions.","commonSituations":"Encountered when input data or configuration does not meet the preconditions of the throwing code path (\"PyString is not valid UTF-8\"). Common cases: missing Cargo feature flags, mismatched dtypes/lengths between arrays or series, out-of-range temporal values, malformed environment variables, or operations on unsupported/complex nested types.","solutions":["The Python string is not valid UTF-8; decode/encode the data as UTF-8 before passing it.","Pass bytes instead of a str if the content is not text."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"5d8ebabf11caea54a5c29178a64a058762f49766","analyzedAt":"2026-08-19T12:15:06.350Z","contentChangedAt":"2026-08-19T12:15:06.350Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}