alibaba/spring-ai-alibaba · error · IllegalArgumentException

Invalid code language provided

Error message

Invalid code language provided

What it means

CodeLanguage.fromValue throws IllegalArgumentException('Invalid code language provided') when the input string is null or blank. It is the enum's guard clause before attempting case-insensitive matching against known language values.

Source

Thrown at spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/node/code/entity/CodeLanguage.java:39

 */
public enum CodeLanguage {

	PYTHON3("python3"), PYTHON("python"), JINJA2("jinja2"), JAVASCRIPT("javascript"), JAVA("java");

	private final String value;

	CodeLanguage(String value) {
		this.value = value;
	}

	public String getValue() {
		return value;
	}

	// Optionally, you can add a method to find an enum constant by its string value
	public static CodeLanguage fromValue(String value) {
		if (value == null || value.trim().isEmpty()) {
			throw new IllegalArgumentException("Invalid code language provided");
		}
		for (CodeLanguage lang : values()) {
			if (lang.getValue().equalsIgnoreCase(value)) {
				return lang;
			}
		}
		throw new IllegalArgumentException("No enum constant " + CodeLanguage.class.getName() + "." + value);
	}

	@Override
	public String toString() {
		return this.value;
	}

}

View on GitHub (pinned to f82da0b50f)

Solutions

  1. Ensure the language string is set and non-blank before calling fromValue.
  2. Provide a sensible default (e.g. 'python') when language is absent.
  3. Catch IllegalArgumentException and report which config field was empty.

Example fix

// before
CodeLanguage lang = CodeLanguage.fromValue(config.getLanguage()); // throws if blank
// after
String language = config.getLanguage();
if (language == null || language.isBlank()) language = "python";
CodeLanguage lang = CodeLanguage.fromValue(language);
Defensive patterns

Strategy: validation

Validate before calling

String lang = rawLanguage == null ? "python" : rawLanguage.trim();
if (lang.isEmpty()) lang = "python";

Type guard

boolean isNonBlankLanguage(String lang) {
    return lang != null && !lang.trim().isEmpty();
}

Try / catch

try {
    return CodeLanguage.fromValue(rawLanguage);
} catch (IllegalArgumentException e) {
    log.warn("Blank/invalid language '{}', defaulting to PYTHON", rawLanguage);
    return CodeLanguage.PYTHON;
}

Prevention

When it happens

Trigger: Calling CodeLanguage.fromValue(null) or fromValue(" ") — typically when a config property or LLM-supplied code block has no language set and the null check at the call site was skipped.

Common situations: YAML/properties config where the language key exists but is empty; LLM code fence without a language tag (```) parsed into an empty string; forgetting to default the language before conversion.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/fdb748345c336d1b. Report an issue: GitHub.