feat(image generation): Add image generation support for models OpenAI and Google Gemini #1009
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Image Generation Feature Implementation Summary
Overview
This PR adds first-class image generation support to langchainrb, addressing issue #924. The implementation follows existing patterns in the codebase while making image generation a provider-agnostic feature that works seamlessly across OpenAI, Google Gemini, and Google Vertex AI.
Key Design Decisions
1. Provider-Agnostic API
generate_imagemethod toLangchain::LLM::Basethat raisesNotImplementedErrorgenerate_image(prompt:, n: 1, ...)2. Model Configuration via Defaults
@defaultshashimage_generation_modelto DEFAULTS for each provider:"dall-e-3""gemini-2.0-flash-preview-image-generation""imagen-3.0-generate-002"3. Response Handling
image_urlsmethodimage_base64smethodImplementation Details
Files Modified
lib/langchain/llm/base.rb
generate_imagemethod that raisesNotImplementedErrorlib/langchain/llm/openai.rb
generate_imageusing OpenAI's Images APIimage_generation_modelto DEFAULTSlib/langchain/llm/google_gemini.rb
generate_imageusing Gemini's generateContent endpointimage_generation_modelto DEFAULTSlib/langchain/llm/google_vertexai.rb
generate_imageusing Vertex AI's Imagen modelimage_generation_modelto DEFAULTSResponse Classes
lib/langchain/llm/response/openai_response.rb
image_urlsmethod to extract URLs from image generation responseslib/langchain/llm/response/google_gemini_response.rb
image_base64smethod to extract base64 data frominlineData.datafieldlib/langchain/llm/response/google_vertex_ai_response.rb (new file)
image_base64smethodExamples and Tests
examples/generate_image.rb
Test Coverage
Usage Example
Benefits
generate_imagefollowing the same patternFuture Considerations
This implementation provides a solid foundation for image generation in langchainrb while maintaining backward compatibility and following established patterns in the codebase.