Skip to content

BedrockRuntimeHints.registerResources() in Spring AI 2.0.0-M1 fails during GraalVM native image AOT processing when attempting to register AWS SDK JSON configuration files from JAR dependencies. #5169

@pdebuitlear

Description

@pdebuitlear

Bug description
During Spring Boot AOT processing for GraalVM native image compilation, the BedrockRuntimeHints class throws an IllegalArgumentException when attempting to register AWS SDK resources:

error when registering Bedrock types
java.lang.IllegalArgumentException: Resource must be a ClassPathResource that exists:
  URL [jar:file://aws-core-2.36.3.jar!/software/amazon/awssdk/awscore/internal/defaults/sdk-default-configuration.json]
  at org.springframework.ai.bedrock.aot.BedrockRuntimeHints.registerResources(BedrockRuntimeHints.java:110)

Impact

  • AOT processing logs contain warnings that may confuse users
  • AWS SDK resources may not be properly registered for native image compilation
  • Potential runtime failures when native image attempts to load AWS SDK configuration files
  • Users must implement workarounds to use Bedrock in native images

Root Cause Analysis

Problematic Code

File: models/spring-ai-bedrock/src/main/java/org/springframework/ai/bedrock/aot/BedrockRuntimeHints.java

Lines 105-112:

private void registerResources(RuntimeHints hints) throws Exception {
    // Register AWS SDK interceptor files
    for (var resource : this.resolver.getResources("classpath*:software/amazon/awssdk/**/*.interceptors")) {
        hints.resources().registerResource(resource);
    }

    // Register AWS SDK JSON config files
    for (var resource : this.resolver.getResources("classpath*:software/amazon/awssdk/**/*.json")) {
        hints.resources().registerResource(resource);  // LINE 110 - FAILS HERE
    }
}

Why It Fails

  1. Resource Pattern Matching:

    • PathMatchingResourcePatternResolver with pattern classpath*:software/amazon/awssdk/**/*.json
    • Finds ALL JSON files in AWS SDK JARs, including:
      • software/amazon/awssdk/awscore/internal/defaults/sdk-default-configuration.json (in aws-core-2.36.3.jar)
      • software/amazon/awssdk/services/bedrockruntime/internal/defaults/sdk-default-configuration.json
      • Many other SDK configuration files
  2. JAR-based Resources vs ClassPathResource:

    • Resources inside JAR files have URLs like: jar:file://aws-core-2.36.3.jar!/software/amazon/awssdk/...
    • These are UrlResource instances (from jar: protocol), NOT ClassPathResource instances
    • Spring's RuntimeHints.resources().registerResource(Resource) validates that the resource is a ClassPathResource
    • When passed a JAR-based UrlResource, it throws IllegalArgumentException
  3. API Misuse:

    • registerResource(Resource) expects file-based classpath resources
    • Should use registerPattern(String) for JAR-based resources

Spring AI's Error Handling

The exception is caught and logged as a warning (lines 66-75):

@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
    try {
        this.registerBedrockRuntimeService(hints);
        this.registerSerializationClasses(hints);
        this.registerResources(hints);
    }
    catch (Throwable ex) {
        this.log.warn("error when registering Bedrock types", ex);  // Just logs warning
    }
}

Environment

  • Spring AI Version: 2.0.0-M1
  • Spring Boot Version: 4.0.0 (milestone)
  • Java Version: 25
  • GraalVM Version: 25
  • AWS SDK Version: 2.36.3 (resolved from Spring AI BOM)
  • Build Tool: Gradle 8.12
  • Native Image Builder: GraalVM native-image (via Spring Boot GraalVM plugin)

Steps to reproduce

  1. Create Spring Boot 4.0.0 project with:

    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-bedrock-spring-boot-starter</artifactId>
        <version>2.0.0-M1</version>
    </dependency>
  2. Configure GraalVM native image plugin:

    plugins {
        id 'org.springframework.boot' version '4.0.0'
        id 'org.graalvm.buildtools.native' version '0.10.4'
    }
  3. Run AOT processing:

    ./gradlew processAot
  4. Observe output:

    WARN  o.s.a.bedrock.aot.BedrockRuntimeHints - error when registering Bedrock types
    java.lang.IllegalArgumentException: Resource must be a ClassPathResource that exists:
      URL [jar:file://aws-core-2.36.3.jar!/software/amazon/awssdk/awscore/internal/defaults/sdk-default-configuration.json]
    

Expected behavior
AWS SDK resources should be registered successfully without warnings or errors during AOT processing.

Actual behavior

  • Warning logged: "error when registering Bedrock types"
  • Full stack trace with IllegalArgumentException
  • AWS SDK resources may not be included in native image
  • Potential runtime failures when AWS SDK tries to load configuration

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions