alibaba/spring-ai-alibaba · error · IllegalArgumentException

Strategy type cannot be null or empty

Error message

Strategy type cannot be null or empty

What it means

registerStrategy derives the registry key from strategy.getStrategyType(); a null or blank type cannot serve as a lookup key, so IllegalArgumentException is thrown. The registry also rejects duplicate types with a separate error ('Strategy type ... is already registered').

Solutions

  1. Implement getStrategyType() to return a fixed non-blank constant (e.g. return "custom-conditional";)
  2. If the type is configurable, validate/trim it in the strategy constructor and fall back to a default
  3. Add a constructor assertion: Objects.requireNonNull(type); if (type.isBlank()) throw ...

Example fix

// before
@Override public String getStrategyType() { return configuredType; }
// after
@Override public String getStrategyType() {
    return (configuredType == null || configuredType.isBlank())
        ? "custom" : configuredType.trim();
}
Defensive patterns

Strategy: validation

Validate before calling

String t = strategy.getStrategyType(); if (t == null || t.trim().isEmpty()) throw new IllegalArgumentException(strategy.getClass().getName() + " returned blank strategy type");

Type guard

static boolean hasValidType(FlowGraphBuildingStrategy s) { String t = s.getStrategyType(); return t != null && !t.trim().isEmpty(); }

Try / catch

try { registry.registerStrategy(strategy); } catch (IllegalArgumentException e) { throw new StrategyRegistrationException(e.getMessage(), e); }

Prevention

When it happens

Trigger: Registering a custom FlowGraphBuildingStrategy whose getStrategyType() returns null, "", or whitespace (e.g. a constant not initialized or a typo returning an empty string).

Common situations: Custom strategy class with getStrategyType() wired to an unset config property; copy-pasted strategy with forgotten type override; internationalization accidentally blanking the constant.

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/30fdaa3f575b1577. Report an issue: GitHub.

Appendix: source

Thrown at spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/flow/strategy/FlowGraphBuildingStrategyRegistry.java:65

	 * @return the registry instance
	 */
	public static FlowGraphBuildingStrategyRegistry getInstance() {
		return INSTANCE;
	}

	/**
	 * Registers a new graph building strategy (same instance returned each time).
	 * @param strategy the strategy to register
	 * @throws IllegalArgumentException if strategy is null or type is already registered
	 */
	public void registerStrategy(FlowGraphBuildingStrategy strategy) {
		if (strategy == null) {
			throw new IllegalArgumentException("Strategy cannot be null");
		}

		String type = strategy.getStrategyType();
		if (type == null || type.trim().isEmpty()) {
			throw new IllegalArgumentException("Strategy type cannot be null or empty");
		}

		if (strategyFactories.containsKey(type)) {
			throw new IllegalArgumentException("Strategy type '" + type + "' is already registered");
		}

		strategyFactories.put(type, () -> strategy);
	}

	/**
	 * Registers a strategy factory. Each call to {@link #createStrategy(String)} or
	 * {@link #getStrategy(String)} will use the factory to obtain a strategy instance.
	 * @param type the strategy type
	 * @param factory the factory that creates strategy instances
	 * @throws IllegalArgumentException if type or factory is null, or type is already registered
	 */
	public void registerStrategy(String type, Supplier<FlowGraphBuildingStrategy> factory) {
		if (type == null || type.trim().isEmpty()) {

View on GitHub (pinned to f82da0b50f)