-
Notifications
You must be signed in to change notification settings - Fork 585
feat: transform raw image responses to json #1067
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Implements provider-specific response formatting for image data. Adds helper functions to identify image providers and format responses correctly based on the provider's requirements, ensuring proper handling of base64 image responses. Improves the robustness of the image handling logic to adapt to different provider responses.
Streamlines the image response transformation process by consolidating functions for image transformation into `getImageTransformer`. Removes unnecessary constants and simplifies the logic to parse image data, resulting in improved readability and maintainability. Enhances logging for better traceability during retry attempts by providing detailed response status information. No functionality changes are introduced; the focus is on code clarity and structure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@unsync I have a few items in mind wrt this PR
- we should be handling transforms only for specific routes and not based on response, so if segmind has a
/v1/images/generations
route, we can do these changes for that route - I'm not entirely keen we should load the entire image file into memory and convert it to json, but that's okay
overall I'm saying this PR could only include changes to provider/segmind/imageGenerate.ts
and provider/segmind/api.ts
and provider/segmind/index.ts
@narengogi if i get this correctly, you would prefer to scope the changes to the providers handlers. Unless i'm missing something, that would not work without a change in the reponseHandler because of this: gateway/src/handlers/responseHandlers.ts Lines 137 to 139 in de327f6
This prevents to reach the code that handles the transformations: gateway/src/handlers/responseHandlers.ts Lines 159 to 166 in de327f6
And before you hit gateway/src/handlers/streamHandler.ts Lines 248 to 250 in de327f6
Since the provider returns a content type Let me know if i missed something here 🙏 And if that doesn't makes it's way to |
I spent some time on this, I was initially misreading what the PR is doing, this fix makes sense for the problem, one change we could do is to convert to json only if routes are in the unified routes like I'm copying @VisargD on this for a review of the approach |
@narengogi @VisargD i made a last commit to the PR to, imho, limit the scope of the behaviour in a more elegant way:
This ensures that :
I also added a try/catch the the transformer wrapper, so that if the transformation fails we return the original response to avoid any failures due to this logic |
Author Description
Summary By MatterAI
🔄 What Changed
This PR adds functionality to transform image content type responses from Segmind to JSON format. It introduces a new handler for converting image responses to base64-encoded JSON, specifically targeting the imageGenerate route. The implementation includes error handling to fall back to the original response if transformation fails.
🔍 Impact of the Change
This change improves the handling of image responses by standardizing them to JSON format, making it easier for clients to process the responses. It's designed as a non-breaking change that enhances the existing functionality without disrupting current behavior.
📁 Total Files Changed
5 files changed with 67 additions and 1 deletion:
🧪 Test Added
No tests have been added in this PR.
🔒 Security Vulnerabilities
No security vulnerabilities detected.
Type of Change
How Has This Been Tested?
Screenshots (if applicable)
N/A
Checklist
Related Issues
N/A
Quality Recommendations
Add unit tests for the new image transformation functionality
Add more detailed error logging in imageToJsonHandler.ts with specific error types
Consider adding a content-type validation function to avoid repetitive checks
Add documentation for the new image transformation feature
Sequence Diagram