The package org.springframework.ai.model is annotated with @NonNullApi

@NonNullApi
@NonNullFields
package org.springframework.ai.model;

import org.springframework.lang.NonNullApi;
import org.springframework.lang.NonNullFields;

So all methods of interface org.springframework.ai.model.ModelResponse are annotated with @NonNull implicitly.

But in ChatResponse implementation, the getResult method could return null:

public Generation getResult() {
    if (CollectionUtils.isEmpty(this.generations)) {
        return null;
    }
    return this.generations.get(0);
}

It breaks IntelliJ IDEA and Kotlin nullability check.

Image

Comment From: Khalil-Elemam

Hi, I was able to reproduce this issue.

In ChatResponse#getResult(), the method currently returns null when generations is empty, which conflicts with the package’s @NonNullApi contract.

I noticed that EmbeddingResponse#getResult() handles the same scenario by throwing an exception, and there was a similar fix applied to ImageResponse#getResult(). I implemented the same approach locally for ChatResponse (and also applied it consistently to ImageResponse) and it resolves the issue.

I have a branch ready — please let me know if you’d like me to open a PR once this is triaged.