Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
fix(nodes): use 0, not null, as the preset node's "defer to the preset" sentinel
Lincoln Stein44a45d42f390
diff --git a/invokeai/app/invocations/text_llm.py b/invokeai/app/invocations/text_llm.pyindex 1d6c99d2e7..3d35f264b3 100644--- a/invokeai/app/invocations/text_llm.py+++ b/invokeai/app/invocations/text_llm.py@@ -127,11 +127,15 @@ class TextLLMWithPresetInvocation(BaseInvocation):description=FieldDescriptions.text_llm_model,ui_model_type=ModelType.TextLLM,)- max_tokens: int | None = InputField(- default=None,- ge=EXPAND_PROMPT_MAX_TOKENS_MIN,+ max_tokens: int = InputField(+ # 0 rather than None as the "defer to the preset" sentinel, matching `tile_size` elsewhere+ # in this package. An `int | None` field serialises its bounds into an OpenAPI `anyOf`,+ # where the editors' field-template builders cannot see them: the node would render with+ # a 0 default they reject, and with no min/max at all.+ default=0,+ ge=0,le=EXPAND_PROMPT_MAX_TOKENS_MAX,- description="Maximum number of tokens to generate. Leave unset to use the preset's own cap, "+ description="Maximum number of tokens to generate. 0 uses the preset's own cap, "f"or {EXPAND_PROMPT_MAX_TOKENS_DEFAULT} if it does not set one.",)seed: int = InputField(default=0, ge=0, le=SEED_MAX, description=FieldDescriptions.seed)@@ -179,15 +183,15 @@ class TextLLMWithPresetInvocation(BaseInvocation):prompt=self.prompt,system_prompt=record.content,# The preset carries its own output-length cap (structured prompts need more room- # than the default), so an unset field defers to it rather than to a fixed number.- # An explicit field value still wins -- it is the node author overriding the preset.+ # than the default), so the 0 default defers to it rather than to a fixed number.+ # Any non-zero field value still wins -- it is the node author overriding the preset.max_tokens=self._resolve_max_tokens(record),seed=self.seed,)return StringOutput(value=output)def _resolve_max_tokens(self, record: SystemPromptRecordDTO) -> int:- if self.max_tokens is not None:+ if self.max_tokens:return self.max_tokensif record.max_tokens is not None:return record.max_tokensdiff --git a/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_09_09_add_system_prompt_max_tokens.py b/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_09_09_add_system_prompt_max_tokens.pyindex da2d1f6228..d880ba130e 100644--- a/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_09_09_add_system_prompt_max_tokens.py+++ b/invokeai/app/services/shared/sqlite_migrator/migrations/migration_2026_09_09_add_system_prompt_max_tokens.py@@ -6,7 +6,7 @@ prompts that are structurally longer by design -- the MiniMax H3 Ref2VA prompt esections and routinely needs three to five times as much.The cap therefore becomes a property of the system prompt: ``max_tokens`` is NULL for every-existing row (meaning "use the default"), and is set to 500 for the Ref2VA prompt.+existing row (meaning "use the default"), and is set to 1000 for the Ref2VA prompt."""import sqlite3diff --git a/invokeai/frontend/web/openapi.json b/invokeai/frontend/web/openapi.jsonindex 005b594fb8..b8e3de9cf1 100644--- a/invokeai/frontend/web/openapi.json+++ b/invokeai/frontend/web/openapi.json@@ -93053,23 +93053,16 @@"ui_model_type": ["text_llm"]},"max_tokens": {- "anyOf": [- {- "maximum": 2048,- "minimum": 1,- "type": "integer"- },- {- "type": "null"- }- ],- "default": null,- "description": "Maximum number of tokens to generate. Leave unset to use the preset's own cap, or 300 if it does not set one.",+ "default": 0,+ "description": "Maximum number of tokens to generate. 0 uses the preset's own cap, or 300 if it does not set one.","field_kind": "input","input": "any",- "orig_default": null,+ "maximum": 2048,+ "minimum": 0,+ "orig_default": 0,"orig_required": false,- "title": "Max Tokens"+ "title": "Max Tokens",+ "type": "integer"},"seed": {"default": 0,diff --git a/invokeai/frontend/web/src/services/api/schema.ts b/invokeai/frontend/web/src/services/api/schema.tsindex a0756ef06d..e2b92cf29e 100644--- a/invokeai/frontend/web/src/services/api/schema.ts+++ b/invokeai/frontend/web/src/services/api/schema.ts@@ -40999,10 +40999,10 @@ export type components = {text_llm_model?: components["schemas"]["ModelIdentifierField"] | null;/*** Max Tokens- * @description Maximum number of tokens to generate. Leave unset to use the preset's own cap, or 300 if it does not set one.- * @default null+ * @description Maximum number of tokens to generate. 0 uses the preset's own cap, or 300 if it does not set one.+ * @default 0*/- max_tokens?: number | null;+ max_tokens?: number;/*** Seed* @description Seed for random number generationdiff --git a/tests/app/invocations/test_text_llm_with_preset.py b/tests/app/invocations/test_text_llm_with_preset.pyindex fd3f692504..553858860c 100644--- a/tests/app/invocations/test_text_llm_with_preset.py+++ b/tests/app/invocations/test_text_llm_with_preset.py@@ -16,11 +16,12 @@ from invokeai.app.invocations.model import ModelIdentifierFieldfrom invokeai.app.invocations.text_llm import TextLLMInvocation, TextLLMWithPresetInvocationfrom invokeai.app.services.system_prompt_records.system_prompt_records_common import (EXPAND_PROMPT_MAX_TOKENS_DEFAULT,+ EXPAND_PROMPT_MAX_TOKENS_MAX,SystemPromptNotFoundError,)-def _make_invocation(prompt_id: str = "test-id", max_tokens: int | None = 50) -> TextLLMWithPresetInvocation:+def _make_invocation(prompt_id: str = "test-id", max_tokens: int = 50) -> TextLLMWithPresetInvocation:return TextLLMWithPresetInvocation(id="test-node",prompt="a cat",@@ -199,7 +200,7 @@ def test_preset_node_skips_the_ownership_check_in_single_user_mode() -> None:def test_preset_node_uses_the_presets_own_cap_when_the_field_is_unset() -> None:# The whole point of a per-prompt cap: a structured preset that needs 500 tokens must not be# truncated at the shared default just because the workflow left the field alone.- inv = _make_invocation(max_tokens=None)+ inv = _make_invocation(max_tokens=0)context = _make_context(prompt_record_content="structured instruction", record_max_tokens=500)with patch("invokeai.app.invocations.text_llm._run_text_llm", return_value="expanded") as mock_run:@@ -209,7 +210,7 @@ def test_preset_node_uses_the_presets_own_cap_when_the_field_is_unset() -> None:def test_preset_node_falls_back_to_the_default_when_neither_names_a_cap() -> None:- inv = _make_invocation(max_tokens=None)+ inv = _make_invocation(max_tokens=0)context = _make_context(prompt_record_content="instruction", record_max_tokens=None)with patch("invokeai.app.invocations.text_llm._run_text_llm", return_value="expanded") as mock_run:@@ -227,3 +228,22 @@ def test_an_explicit_field_value_overrides_the_presets_cap() -> None:inv.invoke(context)assert mock_run.call_args.kwargs["max_tokens"] == 50+++def test_the_unset_sentinel_is_the_field_default_and_is_accepted() -> None:+ # The editors materialise every input from the schema default, so the sentinel has to be a+ # value the node itself accepts -- an `int | None` field defaults to null in the schema, which+ # both editors turn into 0 and the node would then reject.+ field = TextLLMWithPresetInvocation.model_fields["max_tokens"]+ assert field.default == 0+ assert _make_invocation(max_tokens=field.default).max_tokens == 0+++def test_the_field_publishes_its_bounds_at_the_top_level_of_the_schema() -> None:+ # Bounds nested inside an `anyOf` are invisible to both editors' field-template builders,+ # which leaves the node's number input unbounded.+ prop = TextLLMWithPresetInvocation.model_json_schema()["properties"]["max_tokens"]+ assert "anyOf" not in prop+ assert prop["minimum"] == 0+ assert prop["maximum"] == EXPAND_PROMPT_MAX_TOKENS_MAX+ assert prop["default"] == 0