Summary
When using Spring AI with custom providers (or providers that include additional fields such as usage cost, billing, model metadata, etc.), the framework does not currently expose the raw provider response or a structured way to access custom fields that are not mapped into Spring AI’s default response models.
This limits the ability to read provider-specific metadata (e.g., cost fields) that exist in the underlying API response but are not mapped by Spring AI.
Problem
Many LLM providers return additional fields in their JSON response. Examples: - token-level cost breakdown - provider usage metadata - custom domain-specific values - provider-specific debugging fields
Spring AI currently:
- parses responses into internal domain classes (ChatResponse, Generation,
Metadata, etc.)
- but drops unrecognized fields
- and does not expose the raw JSON returned by the provider
Because of this, there is currently no way to access provider-specific
metadata, even via Metadata, Advisors, or Interceptors.
What I Tried
- Using Advisors (
CallAdvisor,StreamAdvisorChain, etc.) — they provide access only to Spring AI domain models, not raw JSON. - Custom
Observationhandlers — still no access to unmapped fields. - Looking for Interceptors in the OpenAI client — not available.
- Checking if
Metadatapreserves unknown fields — it does not.
Requested Feature
I’d like Spring AI to provide one or both of the following:
1. Access to the raw provider response
For example: ```java ChatResponse response = client.call(prompt); String rawJson = response.rawResponse();