{"record":{"id":"4c31158224fb0421","repo":"huggingface/tokenizers","slug":"uninitialized-encoding","errorCode":null,"errorMessage":"Uninitialized Encoding","messagePattern":"Uninitialized Encoding","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"bindings/node/src/encoding.rs","lineNumber":88,"sourceCode":"      JsTruncationStrategy::OnlyFirst => tokenizers::TruncationStrategy::OnlyFirst,\n      JsTruncationStrategy::OnlySecond => tokenizers::TruncationStrategy::OnlySecond,\n    }\n  }\n}\n\n#[napi]\nimpl JsEncoding {\n  #[napi(constructor)]\n  pub fn new() -> Self {\n    Self { encoding: None }\n  }\n\n  #[napi]\n  pub fn get_length(&self) -> u32 {\n    self\n      .encoding\n      .as_ref()\n      .expect(\"Uninitialized Encoding\")\n      .get_ids()\n      .len() as u32\n  }\n\n  #[napi]\n  pub fn get_n_sequences(&self) -> u32 {\n    self\n      .encoding\n      .as_ref()\n      .expect(\"Uninitialized Encoding\")\n      .n_sequences() as u32\n  }\n\n  #[napi]\n  pub fn get_ids(&self) -> Vec<u32> {\n    self\n      .encoding\n      .as_ref()","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/huggingface/tokenizers/blob/6cfd9d385ca0ed91c10b49f0ce97d02cfde1b607/bindings/node/src/encoding.rs#L70-L106","documentation":"The Node binding wraps the Rust Encoding in an Option that is only populated after a tokenizer `encode` call or explicit construction. `get_length` unwraps it with `.expect(\"Uninitialized Encoding\")`, so calling this accessor on an Encoding object whose inner value was never set panics with 'Uninitialized Encoding'.","triggerScenarios":"Creating an empty Encoding object via the N-API constructor (e.g. `new encoding.Encoding()` in the bindings' own test/util code, or an Encoding returned in an uninitialized state) and then calling `getLength()` before it has been assigned a real encoding.","commonSituations":"Binding-internal tests or wrapper code that instantiates Encoding directly; deserialization/interop paths that hand back an Encoding placeholder without running encode; holding a reference to an encoding whose underlying value failed to initialize.","solutions":["Only call get_length on encodings produced by `tokenizer.encode(...)` or `tokenizer.encodeBatch(...)`.","If constructing Encoding manually (binding-internal code), pass a valid Rust Encoding to the constructor instead of leaving the Option None.","Wrap the call in try/catch in JS, since expect() panics surface as a thrown N-API error.","Check for null/undefined or an 'initialized' flag on the object before accessing encoding methods."],"exampleFix":"// before\nconst enc = new Encoding();\nconsole.log(enc.getLength()); // panic: Uninitialized Encoding\n// after\nconst enc = tokenizer.encode(\"hello world\");\nconsole.log(enc.length); // valid: 2","handlingStrategy":"type-guard","validationCode":"function encodingIsUsable(enc) {\n  return enc != null && typeof enc.getLength === \"function\" && enc._initialized !== false;\n}\nif (encodingIsUsable(enc)) console.log(enc.getLength());","typeGuard":"function isInitializedEncoding(obj) {\n  return obj instanceof Encoding && obj._encoding != null;\n}","tryCatchPattern":"let length;\ntry {\n  length = enc.getLength();\n} catch (err) {\n  if (String(err.message).includes(\"Uninitialized Encoding\")) {\n    length = 0;\n  } else {\n    throw err;\n  }\n}","preventionTips":["Only use Encoding objects returned by tokenizer.encode/encodeBatch.","Never instantiate the binding's Encoding class directly in application code.","Wrap accessor calls in try/catch in wrapper libraries that may see placeholder encodings."],"tags":["node","rust","napi","uninitialized","panic"],"backgroundTag":"internal-invariant-violation","analyzedSha":"6cfd9d385ca0ed91c10b49f0ce97d02cfde1b607","analyzedAt":"2026-09-09T11:43:25.027Z","contentChangedAt":"2026-09-09T11:43:25.027Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}