spring-projects/spring-ai · error · java.lang.IllegalArgumentException

Region is empty and cannot be loaded from DefaultAwsRegionPr

Error message

Region is empty and cannot be loaded from DefaultAwsRegionProviderChain: 

What it means

AbstractBedrockApi requires an AWS region. When the Region parameter is empty/null, it attempts to resolve one through the AWS SDK's DefaultAwsRegionProviderChain (env vars, system properties, profile, EC2/ECS metadata). If that chain also fails with SdkClientException, the constructor throws IllegalArgumentException because a region is mandatory to call Bedrock.

Source

Thrown at models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/api/AbstractBedrockApi.java:335

					}
					eventSink.emitError(error, DEFAULT_EMIT_FAILURE_HANDLER);
				})
				.onEventStream(stream -> stream.subscribe(
						(ResponseStream e) -> e.accept(visitor)))
				.build();

		this.clientStreaming.invokeModelWithResponseStream(invokeRequest, responseHandler);

		return eventSink.asFlux();
	}

	private Region getRegion(Region region) {
		if (ObjectUtils.isEmpty(region)) {
			try {
				return DefaultAwsRegionProviderChain.builder().build().getRegion();
			}
			catch (SdkClientException e) {
				throw new IllegalArgumentException("Region is empty and cannot be loaded from DefaultAwsRegionProviderChain: " + e.getMessage(), e);
			}
		}
		else {
			return region;
		}
	}

	/**
	 * Encapsulates the metrics about the model invocation.
	 * https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html
	 *
	 * @param inputTokenCount The number of tokens in the input prompt.
	 * @param firstByteLatency The time in milliseconds between the request being sent and the first byte of the
	 * response being received.
	 * @param outputTokenCount The number of tokens in the generated text.
	 * @param invocationLatency The time in milliseconds between the request being sent and the response being received.
	 */
	@JsonInclude(Include.NON_NULL)

View on GitHub (pinned to 98a7beda4f)

Solutions

  1. Set the AWS_REGION (or AWS_DEFAULT_REGION) environment variable, e.g. export AWS_REGION=us-east-1
  2. Configure the region explicitly in the Spring AI property (e.g. spring.ai.bedrock.aws.region) or pass Region.of("us-east-1") to the constructor
  3. Set up an AWS profile in ~/.aws/config or ensure the runtime environment provides instance metadata

Example fix

// before
var api = new BedrockTitanEmbeddingApi(modelId, null); // no region anywhere
// after
var api = new BedrockTitanEmbeddingApi(modelId, Region.US_EAST_1);
Defensive patterns

Strategy: validation

Validate before calling

String region = System.getenv("AWS_REGION");
if (region == null && new File(System.getProperty("user.home") + "/.aws/config").exists() == false) {
    throw new IllegalStateException("Set AWS_REGION or spring.ai.bedrock.aws.region before creating the Bedrock client");
}

Try / catch

try { new BedrockTitanEmbeddingApi(modelId, null); } catch (IllegalArgumentException e) { if (e.getMessage().contains("Region is empty")) { /* prompt for region config */ } throw e; }

Prevention

When it happens

Trigger: Constructing any AbstractBedrockApi subclass (or the Spring AI auto-configured client) without a Region and without any resolvable default region source (no AWS_REGION/AWS_DEFAULT_REGION env var, no ~/.aws/config profile, no instance metadata).

Common situations: Running locally without AWS CLI profile configured; deploying outside AWS without setting the region; passing null Region programmatically; containers missing AWS_REGION env var.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of spring-projects/spring-ai@98a7beda4f (2026-09-11). Data as JSON: /api/errors/55f68cc17202ed62. Report an issue: GitHub.