spring-projects / spring-projects/spring-ai

OpenAI API returns body as empty

Open
#3,528 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage
Dominant language
Java
Stars
9.5k
Forks
2.9k
Avg merge
1d 7h
Merged PRs (30d)
6

Description

Please do a quick search on GitHub issues first, there might be already a duplicate issue for the one you are about to create.
If the bug is trivial, just go ahead and create the issue. Otherwise, please take a few moments and fill in the following sections:

Bug description
There is a problem calling my private cloud OpenAI service using OpenAiApi

Image
But using openai4j is normal

Image
Using Postman is also normal

Image

Environment
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring-framework.version>6.2.4</spring-framework.version>
<spring-boot.version>3.4.3</spring-boot.version>
<spring-ai.version>1.0.0-M6</spring-ai.version>
<spring-cloud.version>2024.0.0</spring-cloud.version>
Steps to reproduce

    @Test
    public void openaiTest() {
        OpenAiApi openAiApi = OpenAiApi.builder()
                .apiKey("")
                .baseUrl("")
                .completionsPath("chat/completions")
                .build();

        try {
            OpenAiApi.ChatCompletionRequest chatRequest = new OpenAiApi.ChatCompletionRequest(
                    Arrays.asList(new OpenAiApi.ChatCompletionMessage("你是谁", OpenAiApi.ChatCompletionMessage.Role.USER))
                    , "DeepSeek-R1", 0.7, true);
            Flux<OpenAiApi.ChatCompletionChunk> chatCompletionChunkFlux = openAiApi.chatCompletionStream(chatRequest);
            System.out.println(chatCompletionChunkFlux.blockLast());
        } catch (WebClientResponseException e) {
            System.err.println(e.getResponseBodyAsString());

            e.printStackTrace();
        }
    }
<dependency>
            <groupId>dev.ai4j</groupId>
            <artifactId>openai4j</artifactId>
            <version>0.17.0</version>
        </dependency>
    @Test
    public void test1() {

        OpenAiClient client = OpenAiClient.builder()
                .baseUrl("")
                .openAiApiKey("")
                .logStreamingResponses()
                .logLevel(LogLevel.DEBUG)
                .logRequests()
                .logResponses()
                // other customizations coming soon!
                .build();

        ChatCompletionRequest request = ChatCompletionRequest.builder()
                .model("DeepSeek-R1")
                .stream(true)
                .addUserMessage("你是谁")
                .temperature(0.7)
                .build();

        client.chatCompletion(request)
                .onResponse(response -> {
                    System.out.println(response.content());
                })
                .onError(error -> {
                    System.out.println("Error: " + error.getMessage());
                })
                .execute();
    }

Expected behavior
What problem causes the body to be empty

Minimal Complete Reproducible example
Please provide a failing test or a minimal complete verifiable example that reproduces the issue.
Bug reports that are reproducible will take priority in resolution over reports that are not reproducible.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the OpenAiApi.chatCompletionStream call in the openaiTest example and compare its behavior with the openai4j test1 example. Reproduce the request against the private cloud service using the supplied Java and Spring AI versions, then inspect the response handling and establish a failing test that captures the empty body.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
ai, api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.