Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import java.util.Objects;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.DeserializationFeature;
Expand All @@ -41,7 +42,7 @@
import org.springframework.ai.util.JacksonUtils;
import org.springframework.core.KotlinDetector;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;

import static org.springframework.ai.util.LoggingMarkers.SENSITIVE_DATA_MARKER;

Expand Down Expand Up @@ -105,7 +106,8 @@ public BeanOutputConverter(Class<T> clazz, ObjectMapper objectMapper) {
* @param objectMapper Custom object mapper for JSON operations.
* @param textCleaner Custom text cleaner for preprocessing responses.
*/
public BeanOutputConverter(Class<T> clazz, ObjectMapper objectMapper, ResponseTextCleaner textCleaner) {
public BeanOutputConverter(Class<T> clazz, @Nullable ObjectMapper objectMapper,
@Nullable ResponseTextCleaner textCleaner) {
this(ParameterizedTypeReference.forType(clazz), objectMapper, textCleaner);
}

Expand Down Expand Up @@ -135,8 +137,8 @@ public BeanOutputConverter(ParameterizedTypeReference<T> typeRef, ObjectMapper o
* @param objectMapper Custom object mapper for JSON operations.
* @param textCleaner Custom text cleaner for preprocessing responses.
*/
public BeanOutputConverter(ParameterizedTypeReference<T> typeRef, ObjectMapper objectMapper,
ResponseTextCleaner textCleaner) {
public BeanOutputConverter(ParameterizedTypeReference<T> typeRef, @Nullable ObjectMapper objectMapper,
@Nullable ResponseTextCleaner textCleaner) {
this(typeRef.getType(), objectMapper, textCleaner);
}

Expand All @@ -148,7 +150,8 @@ public BeanOutputConverter(ParameterizedTypeReference<T> typeRef, ObjectMapper o
* @param objectMapper Custom object mapper for JSON operations. endings.
* @param textCleaner Custom text cleaner for preprocessing responses.
*/
private BeanOutputConverter(Type type, ObjectMapper objectMapper, ResponseTextCleaner textCleaner) {
private BeanOutputConverter(Type type, @Nullable ObjectMapper objectMapper,
@Nullable ResponseTextCleaner textCleaner) {
Objects.requireNonNull(type, "Type cannot be null;");
this.type = type;
this.objectMapper = objectMapper != null ? objectMapper : getObjectMapper();
Expand Down Expand Up @@ -217,14 +220,13 @@ private void generateSchema() {
* @param text The LLM output in string format.
* @return The parsed output in the desired target type.
*/
@SuppressWarnings("unchecked")
@Override
public T convert(@NonNull String text) {
public T convert(String text) {
try {
// Clean the text using the configured text cleaner
text = this.textCleaner.clean(text);

return (T) this.objectMapper.readValue(text, this.objectMapper.constructType(this.type));
return this.objectMapper.readValue(text, this.objectMapper.constructType(this.type));
}
catch (JsonProcessingException e) {
logger.error(SENSITIVE_DATA_MARKER,
Expand Down Expand Up @@ -272,12 +274,16 @@ public String getJsonSchema() {

public Map<String, Object> getJsonSchemaMap() {
try {
return this.objectMapper.readValue(this.jsonSchema, Map.class);
return this.objectMapper.readValue(this.jsonSchema, new MapTypeReference());
}
catch (JsonProcessingException ex) {
logger.error("Could not parse the JSON Schema to a Map object", ex);
throw new IllegalStateException(ex);
}
}

private static final class MapTypeReference extends TypeReference<Map<String, Object>> {

}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2023-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,9 +33,6 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.slf4j.LoggerFactory;

import org.springframework.ai.util.TextBlockAssertion;
Expand All @@ -52,17 +49,12 @@
* @author Soby Chacko
* @author Konstantin Pavlov
*/
@ExtendWith(MockitoExtension.class)
class BeanOutputConverterTest {

private ListAppender<ILoggingEvent> logAppender;

@Mock
private ObjectMapper objectMapperMock;

@BeforeEach
void beforeEach() {

var logger = (Logger) LoggerFactory.getLogger(BeanOutputConverter.class);

this.logAppender = new ListAppender<>();
Expand All @@ -83,19 +75,15 @@ static class TestClass {

private String someString;

@SuppressWarnings("unused")
TestClass() {
}

TestClass(String someString) {
this.someString = someString;
}

String getSomeString() {
return this.someString;
}

public void setSomeString(String someString) {
@SuppressWarnings("unused")
void setSomeString(String someString) {
this.someString = someString;
}

Expand All @@ -105,19 +93,15 @@ static class TestClassWithDateProperty {

private LocalDate someString;

@SuppressWarnings("unused")
TestClassWithDateProperty() {
}

TestClassWithDateProperty(LocalDate someString) {
this.someString = someString;
}

LocalDate getSomeString() {
return this.someString;
}

public void setSomeString(LocalDate someString) {
@SuppressWarnings("unused")
void setSomeString(LocalDate someString) {
this.someString = someString;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -41,7 +41,7 @@ void shouldApplyCleanersInOrder() {

@Test
void shouldWorkWithSingleCleaner() {
var cleaner = new CompositeResponseTextCleaner(text -> text.trim());
var cleaner = new CompositeResponseTextCleaner(String::trim);
String result = cleaner.clean(" content ");
assertThat(result).isEqualTo("content");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 the original author or authors.
* Copyright 2025-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down