langgenius/dify · error · ValueError
Invalid type: {req_data.type}
Error message
Invalid type: {req_data.type} What it means
ValueError raised by InstructionGenerationTemplateApi.post when req_data.type does not match 'prompt' or 'code' in the match/case. Only those two template types are defined. Flask maps the uncaught ValueError to a 400 response with the message including the offending value.
Source
Thrown at api/controllers/console/app/generator.py:446
@console_ns.expect(console_ns.models[InstructionTemplatePayload.__name__])
@console_ns.response(200, "Template retrieved successfully", console_ns.models[SimpleDataResponse.__name__])
@console_ns.response(400, "Invalid request parameters")
@setup_required
@login_required
@account_initialization_required
@model_validate(InstructionTemplatePayload)
def post(self, req_data: InstructionTemplatePayload):
match req_data.type:
case "prompt":
from core.llm_generator.prompts import INSTRUCTION_GENERATE_TEMPLATE_PROMPT
return {"data": INSTRUCTION_GENERATE_TEMPLATE_PROMPT}
case "code":
from core.llm_generator.prompts import INSTRUCTION_GENERATE_TEMPLATE_CODE
return {"data": INSTRUCTION_GENERATE_TEMPLATE_CODE}
case _:
raise ValueError(f"Invalid type: {req_data.type}")
def _workflow_instruction_guard(args: WorkflowGeneratePayload) -> tuple[dict, int] | None:
"""Shared boundary guard for the workflow-generate endpoints.
Returns a ``(body, 400)`` tuple when the instruction is empty / whitespace
or either free-text field exceeds the cap, else ``None``. Pydantic only
validates the field is a str; a whitespace-only or pasted-document input
would otherwise waste a slow planner+builder roundtrip on a response the
validator rejects anyway. Both the blocking and streaming endpoints call
this so they reject identical inputs.
"""
if not args.instruction.strip():
return {
"error": "Instruction is required",
"errors": [{"code": WorkflowGenerateErrorCode.EMPTY_INSTRUCTION, "detail": "Instruction is required"}],
}, 400
if len(args.instruction) > _MAX_INSTRUCTION_LENGTH or len(args.ideal_output) > _MAX_INSTRUCTION_LENGTH:View on GitHub (pinned to ef8544b173)
Solutions
- Send type='prompt' or type='code' in the request body.
- If you need a new template type, extend the match/case in generator.py and add the template constant in core/llm_generator/prompts.py.
- Add a client-side enum/dropdown limited to the two valid values to prevent invalid submissions.
Example fix
// before
POST /instruction-generate/template { "type": "text" }
// after
POST /instruction-generate/template { "type": "prompt" } Defensive patterns
Strategy: type-guard
Validate before calling
const VALID_TEMPLATE_TYPES = new Set(['prompt', 'code']);
function isValidTemplateType(t: string): boolean {
return VALID_TEMPLATE_TYPES.has(t);
}
// guard before send
if (!isValidTemplateType(payload.type)) throw new Error(`type must be prompt or code, got ${payload.type}`); Type guard
type InstructionTemplateType = 'prompt' | 'code';
function isInstructionTemplateType(t: string): t is InstructionTemplateType {
return t === 'prompt' || t === 'code';
} Prevention
- Drive the type field from a constrained dropdown, never free text.
- Validate client-side against the literal union before posting.
- If extending the vocabulary, update both client and server match/case in lockstep.
When it happens
Trigger: POST /console/api/apps/<app_id>/instruction-generate/template with a body whose 'type' field is anything other than 'prompt' or 'code' (e.g. 'text', 'json', empty, or a typo like 'promt').
Common situations: Client sending a wrong type string; frontend regression that stopped constraining the dropdown; API consumer guessing the field vocabulary.
Related errors
- unsupported argument value
- metadata must be one of all, only, without
- rating must be either 'like' or 'dislike'
- Invalid status
- streaming response body missing
AI-assisted analysis of langgenius/dify@ef8544b173 (2026-08-12).
Data as JSON: /api/errors/85dfd7b1bdbee696.
Report an issue: GitHub.