{"record":{"id":"033b9d56022bd690","repo":"pola-rs/polars","slug":"creating-extension-types-not-allowed-try-setting","errorCode":null,"errorMessage":"creating extension types not allowed - try setting the environment variable {env}","messagePattern":"creating extension types not allowed - try setting the environment variable (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-core/src/chunked_array/object/extension/mod.rs","lineNumber":69,"sourceCode":"            (drop_fn)()\n        }\n    }\n}\n\n// https://stackoverflow.com/questions/28127165/how-to-convert-struct-to-u8d\n// not entirely sure if padding bytes in T are initialized or not.\nunsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {\n    std::slice::from_raw_parts((p as *const T) as *const u8, size_of::<T>())\n}\n\n/// Create an extension Array that can be sent to arrow and (once wrapped in [`PolarsExtension`] will\n/// also call drop on `T`, when the array is dropped.\npub(crate) fn create_extension<I: Iterator<Item = Option<T>> + TrustedLen, T: Sized + Default>(\n    iter: I,\n) -> PolarsExtension {\n    let env = \"POLARS_ALLOW_EXTENSION\";\n    if !(POLARS_ALLOW_EXTENSION.load() || std::env::var(env).is_ok()) {\n        panic!(\"creating extension types not allowed - try setting the environment variable {env}\")\n    }\n    let t_size = size_of::<T>();\n    let t_alignment = align_of::<T>();\n    let n_t_vals = iter.size_hint().1.unwrap();\n\n    let mut buf = Vec::with_capacity(n_t_vals * t_size);\n    let mut validity = BitmapBuilder::with_capacity(n_t_vals);\n\n    // when we transmute from &[u8] to T, T must be aligned correctly,\n    // so we pad with bytes until the alignment matches\n    let n_padding = (buf.as_ptr() as usize) % t_alignment;\n    buf.extend(std::iter::repeat_n(0, n_padding));\n\n    // transmute T as bytes and copy in buffer\n    for opt_t in iter.into_iter() {\n        match opt_t {\n            Some(t) => {\n                unsafe {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/pola-rs/polars/blob/5d8ebabf11caea54a5c29178a64a058762f49766/crates/polars-core/src/chunked_array/object/extension/mod.rs#L51-L87","documentation":"Safety gate around Polars' internal extension-type mechanism (create_extension in the object/extension module): registering/creating custom extension types is disabled unless the opt-in environment variable (POLARS_ALLOW_EXTENSION) is set, because creating extension arrays from untrusted input is unsafe.","triggerScenarios":"This panic/expect fires when execution reaches an unguarded state described by: \"creating extension types not allowed - try setting the environment variable {env}\". 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 (\"creating extension types not allowed - try setting the environment variable {env}\"). 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":["Set the indicated environment variable (e.g. POLARS_ALLOW_EXTENSION...) to opt in to creating extension/object types.","Avoid constructing extension types in untrusted/production code; they can carry unsafe invariants."],"exampleFix":null,"handlingStrategy":"validation","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-14T00:17:10.932Z"}