spring-projects / spring-projects/spring-ai

Backport per-request custom header application for openai-sdk

Open
#5,939 0 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

In https://github.com/spring-projects/spring-ai/commit/f36dce3bd22c9e20159fc715d23a2da88b610f1e included in the current 2.0.0-M5, the application of custom headers from prompt chat options was added for openai(-sdk) based chat models.

/*
 * org.springframework.ai.openaisdk.OpenAiSdkChatModel#createRequest (2.0.0-M5)
 */
ChatCompletionCreateParams createRequest(Prompt prompt, boolean stream) {

  // ...
  
  if (requestOptions.getCustomHeaders() != null && !requestOptions.getCustomHeaders().isEmpty()) {
      requestOptions.getCustomHeaders().forEach(builder::putAdditionalHeader);
  }

  // ...

This is a very useful and handy addition, because it allows things like per-request authorization request headers with only a single chat model/client.

final String content = chatClient
    .prompt(Prompt.builder()
        .content("How are you?")
        .chatOptions(OpenAiChatOptions.builder().customHeaders(Map.of(
                HttpHeaders.AUTHORIZATION, "Bearer some-tenant-specific-key"
            ))
            .build())
        .build())
    .call();

Because this is such a useful change I would kindly request to backport it to 1.1.6.

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 by comparing the 1.1.6 openai(-sdk) chat model implementation with org.springframework.ai.openaisdk.OpenAiSdkChatModel#createRequest and commit f36dce3bd22c9e20159fc715d23a2da88b610f1e. Trace how Prompt chat options reach the request builder, then verify that custom headers are applied per request in the 1.1.6 code path.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.