From 3fa0e8c9274bbd52a528027d09c2f76dbabed465 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 23:03:17 +0000 Subject: [PATCH] Refactor validation into shared helper function and improve type handling Co-authored-by: jags111 <5968619+jags111@users.noreply.github.com> --- efficiency_nodes.py | 7 +++---- py/bnk_adv_encode.py | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/efficiency_nodes.py b/efficiency_nodes.py index 803fa28..c3ff423 100644 --- a/efficiency_nodes.py +++ b/efficiency_nodes.py @@ -53,6 +53,7 @@ from .py import city96_latent_upscaler from .py import ttl_nn_latent_upscaler from .py import bnk_tiled_samplers from .py import bnk_adv_encode +from .py.bnk_adv_encode import normalize_prompt_text sys.path.remove(my_dir) from comfy import samplers @@ -73,10 +74,8 @@ def encode_prompts(positive_prompt, negative_prompt, token_normalization, weight return_type="both"): # Ensure prompts are valid strings to prevent tokenization errors - if positive_prompt is None or (isinstance(positive_prompt, str) and not positive_prompt.strip()): - positive_prompt = " " - if negative_prompt is None or (isinstance(negative_prompt, str) and not negative_prompt.strip()): - negative_prompt = " " + positive_prompt = normalize_prompt_text(positive_prompt) + negative_prompt = normalize_prompt_text(negative_prompt) positive_encoded = negative_encoded = refiner_positive_encoded = refiner_negative_encoded = None diff --git a/py/bnk_adv_encode.py b/py/bnk_adv_encode.py index 9b2e001..2b97df6 100644 --- a/py/bnk_adv_encode.py +++ b/py/bnk_adv_encode.py @@ -6,6 +6,21 @@ from math import gcd from comfy import model_management from comfy.sdxl_clip import SDXLClipModel, SDXLRefinerClipModel, SDXLClipG +def normalize_prompt_text(text): + """ + Normalize prompt text to prevent tokenization errors. + Converts None, empty strings, or whitespace-only strings to a single space. + Ensures the input is a string type. + """ + if text is None: + return " " + if not isinstance(text, str): + # Convert non-string types to string + text = str(text) + if not text.strip(): + return " " + return text + def _grouper(n, iterable): it = iter(iterable) while True: @@ -238,8 +253,7 @@ def prepareXL(embs_l, embs_g, pooled, clip_balance): def advanced_encode(clip, text, token_normalization, weight_interpretation, w_max=1.0, clip_balance=.5, apply_to_pooled=True): # Ensure text is a valid string to prevent tokenization errors - if text is None or (isinstance(text, str) and not text.strip()): - text = " " + text = normalize_prompt_text(text) tokenized = clip.tokenize(text, return_word_ids=True) if isinstance(clip.cond_stage_model, (SDXLClipModel, SDXLRefinerClipModel, SDXLClipG)): embs_l = None @@ -269,10 +283,8 @@ def advanced_encode(clip, text, token_normalization, weight_interpretation, w_ma w_max=w_max) def advanced_encode_XL(clip, text1, text2, token_normalization, weight_interpretation, w_max=1.0, clip_balance=.5, apply_to_pooled=True): # Ensure texts are valid strings to prevent tokenization errors - if text1 is None or (isinstance(text1, str) and not text1.strip()): - text1 = " " - if text2 is None or (isinstance(text2, str) and not text2.strip()): - text2 = " " + text1 = normalize_prompt_text(text1) + text2 = normalize_prompt_text(text2) tokenized1 = clip.tokenize(text1, return_word_ids=True) tokenized2 = clip.tokenize(text2, return_word_ids=True)