Last active
August 16, 2025 21:08
-
-
Save schoblaska/d462ef017f2f8fc331399628fd750630 to your computer and use it in GitHub Desktop.
ActiveModel validation errors make great feedback for LLMs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ChatBlock | |
| DEFAULT_RETRIES = 2 | |
| attr_reader :chat, :retries | |
| def self.wrap(chat, retries: DEFAULT_RETRIES, &block) | |
| new(chat, retries:).execute(&block) | |
| end | |
| def initialize(chat, retries: DEFAULT_RETRIES) | |
| @chat = chat | |
| @retries = retries | |
| end | |
| def execute(&block) | |
| (retries + 1).times do | |
| return ActiveRecord::Base.transaction(&block) | |
| rescue StandardError => e | |
| chat.add_message( | |
| role: "user", | |
| content: "Error: #{e.message}" | |
| ) | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person | |
| include ActiveModel::Validations | |
| attr_accessor :name | |
| validates :name, length: {maximum: 3} | |
| end | |
| chat = RubyLLM.chat | |
| person = Person.new | |
| ChatBlock.wrap(chat) do | |
| person.name = chat.ask("Give me a name").content | |
| person.validate! | |
| end | |
| chat.messages.each { |m| puts "#{m.role}: #{m.content}"} | |
| # user: Give me a name | |
| # | |
| # assistant: Sure! Here are a few options in different styles. If none of | |
| # these fit what you’re looking for, just tell me the vibe (classic, unique, | |
| # gender-neutral, cultural background, etc.) and I can tailor more | |
| # suggestions. | |
| # | |
| # Classic: Eleanor | |
| # Modern: Jaxon | |
| # Gender-neutral: River | |
| # Literary: Atticus | |
| # Mythological: Aria | |
| # Short & punchy: Leo | |
| # | |
| # user: Error: Validation failed: Name is too long (maximum is 3 characters) | |
| # | |
| # user: Give me a name | |
| # | |
| # assistant: Kai |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment