Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(speech): coerce blank panel parameters before validation
trohitgd746ba6e17bc
diff --git a/server/nodes/speech/_base.py b/server/nodes/speech/_base.pyindex bd919800..3960c2e6 100644--- a/server/nodes/speech/_base.py+++ b/server/nodes/speech/_base.py@@ -6,8 +6,9 @@ parts both nodes need and neither should own.from __future__ import annotations+import jsonfrom pathlib import Path-from typing import Any, Dict, Optional, Sequence, Tuple+from typing import Any, Dict, Optional, Sequence, Tuple, Union, get_args, get_originfrom core.logging import get_loggerfrom services.media import coerce_file_param, read_media_bytes, resolve_media@@ -19,6 +20,94 @@ from . import _config as speech_configlogger = get_logger(__name__)+# ---------------------------------------------------------------------------+# Parameter coercion+# ---------------------------------------------------------------------------+#+# The parameter panel stores "" for a field the user has cleared or never+# filled, whatever the declared type. For a `str` field that is harmless, but+# `""` against `Optional[float]`, `bool` or `Dict[str, Any]` is a hard+# validation error -- "Input should be a valid dictionary" being the one that+# surfaced first.+#+# It is not enough to fix the dict field: `speed`, `sample_rate`, `translate`,+# `diarize` and `timestamps` all fail the same way. So the rule is general --+# a blank string against a field that cannot hold a string means "unset", and+# is dropped so the field's own default applies.+#+# Same class of problem the repo already solves per-node with+# `AndroidServiceParams._coerce_parameters` and `WriteTodosParams._coerce_todos`;+# this is the reusable form.+++def _accepts_str(annotation: Any) -> bool:+ """Whether a blank string is a legitimate value for this annotation.++ `Dict[str, Any]` is the case worth spelling out: its `get_args` are+ `(str, Any)`, so a naive "is str among the args" check would wrongly+ conclude a dict field accepts a string. Container origins are rejected+ before the args are consulted.+ """+ if annotation is Any or annotation is str:+ return True+ origin = get_origin(annotation)+ if origin is Union:+ return any(_accepts_str(arg) for arg in get_args(annotation))+ if origin is not None:+ return False+ return False+++def coerce_blank_params(cls: Any, values: Any, *, object_fields: Sequence[str] = ()) -> Any:+ """Normalize panel-supplied blanks before Pydantic validates them.++ Fields named in ``object_fields`` additionally accept a JSON **object**+ string, because the panel has no object widget and renders them as a text+ input -- so a user who types `{"instructions": "cheerful"}` gets what they+ plainly meant rather than a type error.+ """+ if not isinstance(values, dict):+ return values++ cleaned: Dict[str, Any] = {}+ for key, value in values.items():+ if key in object_fields:+ cleaned[key] = _coerce_object(key, value)+ continue++ field = cls.model_fields.get(key)+ blank = isinstance(value, str) and not value.strip()+ if blank and field is not None and not _accepts_str(field.annotation):+ continue # drop -> the field default applies+ cleaned[key] = value+ return cleaned+++def _coerce_object(key: str, value: Any) -> Dict[str, Any]:+ if isinstance(value, dict):+ return value+ if value is None:+ return {}+ if isinstance(value, str):+ text = value.strip()+ if not text:+ return {}+ try:+ parsed = json.loads(text)+ except ValueError as exc:+ raise ValueError(+ f"{key} must be a JSON object, e.g. "+ '{"instructions": "speak cheerfully"}. Could not parse it: '+ f"{exc}."+ ) from exc+ if isinstance(parsed, dict):+ return parsed+ raise ValueError(+ f"{key} must be a JSON object, not a {type(parsed).__name__}."+ )+ raise ValueError(f"{key} must be a JSON object, not a {type(value).__name__}.")++async def provider_api_key(ctx: NodeContext, provider: str) -> str:"""Resolve the stored key for ``provider`` through its credential.@@ -151,6 +240,7 @@ async def track_usage(__all__ = [+ "coerce_blank_params","measure_seconds","provider_api_key","read_audio_input",diff --git a/server/nodes/speech/speech_to_text.py b/server/nodes/speech/speech_to_text.pyindex 26047fd2..c95247ad 100644--- a/server/nodes/speech/speech_to_text.py+++ b/server/nodes/speech/speech_to_text.py@@ -21,7 +21,7 @@ from __future__ import annotationsfrom typing import Any, Dict, List, Optional-from pydantic import BaseModel, Field+from pydantic import BaseModel, Field, model_validatorfrom services.plugin import ActionNode, NodeContext, Operation, TaskQueue@@ -29,6 +29,7 @@ from ..model._credentials import GroqCredential, OpenAICredential, SarvamCredentfrom . import _config as speech_configfrom . import _unifierfrom ._base import (+ coerce_blank_params,measure_seconds,provider_api_key,read_audio_input,@@ -99,13 +100,21 @@ class SpeechToTextParams(BaseModel):provider_options: Dict[str, Any] = Field(default_factory=dict,description=(- "Vendor-specific options passed through untouched — e.g. Deepgram "- "keyterm/redact, Sarvam mode, OpenAI response_format."+ "Vendor-specific options as a JSON object, passed through "+ 'untouched — e.g. {"keyterm": ["invoice"]} for Deepgram, '+ '{"mode": "codemix"} for Sarvam, {"response_format": "text"} for OpenAI.'),)model_config = {"extra": "ignore"}+ @model_validator(mode="before")+ @classmethod+ def _coerce_panel_blanks(cls, values: Any) -> Any:+ # The panel stores "" for any cleared field, which is a hard type+ # error against the three booleans and `provider_options`.+ return coerce_blank_params(cls, values, object_fields=("provider_options",))+class SpeechToTextOutput(BaseModel):transcript: str = ""diff --git a/server/nodes/speech/text_to_speech.py b/server/nodes/speech/text_to_speech.pyindex 488d9348..3fae0a78 100644--- a/server/nodes/speech/text_to_speech.py+++ b/server/nodes/speech/text_to_speech.py@@ -27,7 +27,7 @@ from __future__ import annotationsfrom typing import Any, Dict, List, Optional-from pydantic import BaseModel, Field+from pydantic import BaseModel, Field, model_validatorfrom services.media import AudioRef, write_audiofrom services.plugin import ActionNode, NodeContext, NodeUserError, Operation, TaskQueue@@ -35,7 +35,12 @@ from services.plugin import ActionNode, NodeContext, NodeUserError, Operation, Tfrom ..model._credentials import OpenAICredential, SarvamCredentialfrom . import _config as speech_configfrom . import _unifier-from ._base import provider_api_key, require_provider, track_usage+from ._base import (+ coerce_blank_params,+ provider_api_key,+ require_provider,+ track_usage,+)from ._credentials import ElevenLabsCredentialfrom ._protocol import TtsRequestfrom ._registry import tts_providers@@ -110,14 +115,21 @@ class TextToSpeechParams(BaseModel):provider_options: Dict[str, Any] = Field(default_factory=dict,description=(- "Vendor-specific options passed through untouched — e.g. "- "ElevenLabs stability/similarity_boost, Sarvam pitch/loudness, "- "OpenAI instructions."+ "Vendor-specific options as a JSON object, passed through "+ 'untouched — e.g. {"stability": 0.6} for ElevenLabs, '+ '{"pitch": 0.2} for Sarvam, {"instructions": "..."} for OpenAI.'),)model_config = {"extra": "ignore"}+ @model_validator(mode="before")+ @classmethod+ def _coerce_panel_blanks(cls, values: Any) -> Any:+ # The panel stores "" for any cleared field, which is a hard type+ # error against `speed`, `sample_rate` and `provider_options`.+ return coerce_blank_params(cls, values, object_fields=("provider_options",))+class TextToSpeechOutput(BaseModel):audio: Optional[AudioRef] = Field(diff --git a/server/tests/nodes/test_speech.py b/server/tests/nodes/test_speech.pyindex 2e55ac4f..0e2577b9 100644--- a/server/tests/nodes/test_speech.py+++ b/server/tests/nodes/test_speech.py@@ -533,6 +533,99 @@ class TestNodeContract:assert "elevenlabs" in result["error"]+class TestPanelBlankCoercion:+ """The parameter panel stores "" for any field the user cleared.++ Against `str` that is harmless, but against `Optional[float]`, `bool` or+ `Dict[str, Any]` it is a hard validation error — which is how this first+ surfaced, as "Invalid parameters: Input should be a valid dictionary" on+ every run of a freshly-dropped node. Six fields across the two nodes are+ affected, so the fix is general rather than per-field.+ """++ @pytest.mark.parametrize(+ "params_cls,payload",+ [+ (+ "tts",+ {"text": "hi", "speed": "", "sample_rate": "", "provider_options": ""},+ ),+ (+ "stt",+ {+ "audio_file": "a.wav",+ "translate": "",+ "diarize": "",+ "timestamps": "",+ "provider_options": "",+ },+ ),+ ],+ )+ def test_blank_strings_fall_back_to_defaults(self, params_cls, payload):+ from nodes.speech.speech_to_text import SpeechToTextParams+ from nodes.speech.text_to_speech import TextToSpeechParams++ cls = TextToSpeechParams if params_cls == "tts" else SpeechToTextParams+ model = cls(**payload)+ assert model.provider_options == {}++ def test_json_object_string_is_parsed(self):+ """The panel has no object widget, so this arrives as typed text."""+ from nodes.speech.text_to_speech import TextToSpeechParams++ model = TextToSpeechParams(+ text="hi", provider_options='{"instructions": "cheerful"}'+ )+ assert model.provider_options == {"instructions": "cheerful"}++ def test_a_real_dict_passes_through(self):+ from nodes.speech.text_to_speech import TextToSpeechParams++ assert TextToSpeechParams(+ text="hi", provider_options={"pitch": 0.2}+ ).provider_options == {"pitch": 0.2}++ @pytest.mark.parametrize("bad", ["[1, 2]", "not json at all", '"a string"'])+ def test_non_object_json_is_rejected_with_a_usable_message(self, bad):+ from pydantic import ValidationError++ from nodes.speech.text_to_speech import TextToSpeechParams++ with pytest.raises(ValidationError, match="JSON object"):+ TextToSpeechParams(text="hi", provider_options=bad)++ def test_string_fields_are_untouched(self):+ """Only fields that CANNOT hold a string get the blank treatment.++ Dropping a blank `text` would turn a min_length error into a+ confusing "field required".+ """+ from pydantic import ValidationError++ from nodes.speech.text_to_speech import TextToSpeechParams++ with pytest.raises(ValidationError, match="at least 1 character"):+ TextToSpeechParams(text="")++ def test_a_dropped_node_validates_with_its_own_defaults(self):+ """Regression for the reported failure: drop node, hit Run."""+ from services.node_registry import get_node_class++ defaults = {+ name: field.default+ for name, field in get_node_class("textToSpeech").Params.model_fields.items()+ }+ defaults["text"] = "hello"+ # PydanticUndefined for default_factory fields is what the panel would+ # store as "" — simulate that rather than the sentinel.+ payload = {+ k: ("" if repr(v).startswith("PydanticUndefined") else v)+ for k, v in defaults.items()+ }+ get_node_class("textToSpeech").Params(**payload)++class TestAudioInputContainment:"""The Sarvam STT node read the credential store. This must not."""