The token counter was crashing during model loading/unloading (race condition) because it was attempting to access attributes on an object that was momentarily None.
This fix introduces a sequential and robust null check (sd_models.model_data is None or sd_models.model_data.sd_model is None) at the start of the function to safely return 0 tokens during unstable states.
The previous fix (v1) was incomplete, as sd_model.cond_stage_model could still be None during model loading/unloading sequences (observed with XYZ Plot model swapping).
This v2 fix adds a specific check for cond_stage_model to ensure stability in dynamic environments.
This fixes the critical `TypeError: argument of type 'NoneType' is not iterable` that frequently occurs in the token counter when the main Stable Diffusion model is being unloaded or reloaded (e.g., during XYZ Plot or manual model switching).
The error happens because the `get_token_counter` function tries to access `sd_models.model_data.sd_model.cond_stage_model` when `sd_model` is momentarily `None`.
The fix adds a robust check for `sd_models.model_data.sd_model is None` at the start of the function, ensuring the token counter safely returns 0 instead of causing an ASGI application crash across all environments (A1111, Forge/reForge).
This fixes the TypeError that occurs during model switching in sd-webui-reforge and possibly other optimized environments.
The error happens because 'get_token_counter' tries to access 'sd_models.model_data.sd_model.cond_stage_model' before the model is fully loaded (when 'sd_model' is None).
The fix adds a check for the 'None' value to safely return 0 tokens instead of throwing an error.
from together import Together
image = client.images.generate(
prompt="MJ v6, A sensual, full-body anime elf girl with long, flowing silver hair and sparkling highlights, deep red eyes, and a seductive, lustful expression, posing with both hands grasping a green wine bottle, wearing an Egyptian-inspired sheer black skirt with intricate golden embroidery, split front and back panels, and a delicate black silk veil draped over her head, paired with black elbow-length fingerless gloves, elegant golden jewelry, and ornate bracelets, set against a dark fantasy blur background with soft body lighting, detailed anime shading, and line art, strong contrast with dramatic shadows, cinematic lighting, and a Goblin Slayer-inspired aesthetic, rendered in a highly detailed, ultra-sharp, and masterful style, with a focus on photorealistic textures and a close-up composition that emphasizes the subject's intricate features and sensual presence.",
model="black-forest-labs/FLUX.1-dev-lora",
height=768,
width=1024,
seed=8910047,
steps=28,
image_loras=[
{
"path": "https://huggingface.co/strangerzonehf/Flux-Midjourney-Mix2-LoRA",
"scale": 1,
},
],
)
print(image.data[0].url)