spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/index.html
Apache License 2.0
2.99k stars 743 forks source link

Suggestion: StructuredOutputConverter implementations could have configured amount of retries when convert() is failing to parse response #947

Open Sameperson opened 3 months ago

Sameperson commented 3 months ago

Current Behavior

Currently, existing convert() implementations (BeanOutputConverter, ListOutputConverter, MapOutputConverter) throw exceptions when the response can't be parsed.

Proposed Behavior

I saw Spring AI has retry properties: https://docs.spring.io/spring-ai/reference/api/chat/openai-chat.html#_chat_properties But these are for the initial calls to the model if I understood correctly, not the converters.

Would it be a good idea to set the optional amount of retries for the converters too? If parsing fails, we can retry to call the AI model in hopes of getting a better response.

Context

I am getting the wrong output from the model from time to time, so I configured retries in my code. But what do you think about having this feature in Spring AI?

ThomasVitale commented 3 months ago

I like the suggestion. LangChain provides a couple of OutputParsers for dealing with errors:

It would be nice to have something similar for the StructuredOutputConverter infrastructure in Spring AI: