11package com .sap .ai .sdk .foundationmodels .openai ;
22
3+ import static com .sap .ai .sdk .foundationmodels .openai .OpenAiUtils .getOpenAiObjectMapper ;
4+
35import com .fasterxml .jackson .core .type .TypeReference ;
46import com .google .common .annotations .Beta ;
57import java .util .Map ;
@@ -26,28 +28,39 @@ public class OpenAiFunctionCall implements OpenAiToolCall {
2628 @ Nonnull String arguments ;
2729
2830 /**
29- * Returns the arguments as a {@code Map<String, Object>}.
31+ * Parses the arguments, encoded as a JSON string, into a {@code Map<String, Object>}.
3032 *
31- * @return the parsed arguments
33+ * @return a map of the arguments
3234 * @throws IllegalArgumentException if parsing fails
3335 * @since 1.7.0
3436 */
3537 @ Nonnull
3638 public Map <String , Object > getArgumentsAsMap () throws IllegalArgumentException {
37- return OpenAiUtils .parseJson (getArguments (), new TypeReference <>() {});
39+ final var typeReference = new TypeReference <Map <String , Object >>() {};
40+ try {
41+ return getOpenAiObjectMapper ().readValue (getArguments (), typeReference );
42+ } catch (Exception e ) {
43+ throw new IllegalArgumentException (
44+ "Failed to parse JSON string to type " + typeReference .getType (), e );
45+ }
3846 }
3947
4048 /**
41- * Returns the arguments as an object of the specified class .
49+ * Parses the arguments, encoded as a JSON string, into an object of the specified type .
4250 *
4351 * @param clazz the class to convert the arguments to
4452 * @param <T> the type of the class
45- * @return the arguments as an object of the specified class
53+ * @return the parsed arguments as an object
4654 * @throws IllegalArgumentException if parsing fails
4755 * @since 1.7.0
4856 */
4957 @ Nonnull
5058 public <T > T getArgumentsAsObject (@ Nonnull final Class <T > clazz ) throws IllegalArgumentException {
51- return OpenAiUtils .parseJson (getArguments (), clazz );
59+ try {
60+ return getOpenAiObjectMapper ().readValue (getArguments (), clazz );
61+ } catch (Exception e ) {
62+ throw new IllegalArgumentException (
63+ "Failed to parse JSON string to class " + clazz .getTypeName (), e );
64+ }
5265 }
5366}
0 commit comments