Skip to content

Function calling fails to resolve the input type if the @Beans definition doesn't return an explicit java.util.Function<In, Out> #726

@tzolov

Description

@tzolov

Lets define a simple WeatherFunction function:

public record WeatherRequest(String city) {
}

public record WeatherResponse(float temperatureInCelsius) {
}

public static class WeatherFunction implements Function<WeatherRequest, WeatherResponse> {
	@Override
	public WeatherResponse apply(WeatherRequest weatherRequest) {
		return new WeatherResponse(42.0f);
	}
}

If the function @bean is defined to return Function<WeatherRequest, WeatherResponse> like this:

@Bean()
Function<WeatherRequest, WeatherResponse> weatherFunctionDefinition() {
	return new WeatherFunction();
}

then SpringAI properly resolves the WeatherRequest input type.

But if the function @bean return type is the function class itself (e.g. WeatherFunction) like:

@Bean()
WeatherFunction weatherClassDefinition() {
	return new WeatherFunction();
}

Then SpringAI will fail to resolve the input type (resolves to Object class) and the the function call will fail with error like:

  "error": {
    "message": "Invalid schema for function 'weather': schema must be a JSON Schema of 'type: \"object\"', got 'type: \"None\"'.",
    "type": "invalid_request_error",
    "param": "tools[0].function.parameters",
    "code": "invalid_function_parameters"
  }

A solution ensure that the TypeResolverHelper#getFunctionArgumentType properly resolves the input type when the @bean return type is an explicit Function<I, O>

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions