feat(openai): add support for image generation tracking#1511
Open
coletebou wants to merge 1 commit intolangfuse:mainfrom
Open
feat(openai): add support for image generation tracking#1511coletebou wants to merge 1 commit intolangfuse:mainfrom
coletebou wants to merge 1 commit intolangfuse:mainfrom
Conversation
Adds automatic tracing for OpenAI image generation API calls: - images.generate() - images.edit() - images.create_variation() Changes: - Added OpenAiDefinition entries for image methods (sync/async) - Extract image parameters (size, quality, style, response_format, n) - Parse image responses (URLs and base64 wrapped in LangfuseMedia) - Track image count as usage metrics - Handle create_variation() which has no prompt parameter (input=None) Tests: - Added tests for sync/async images.generate() - Added skipped stubs for edit()/create_variation() (require DALL-E 2 + image files)
7154a72 to
f438bb3
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds automatic tracing for OpenAI image generation API calls:
images.generate()images.edit()images.create_variation()Changes
langfuse/openai.pyOpenAiDefinitionentries for image methods (sync and async variants)_get_langfuse_data_from_kwargs()to extract image generation parameters (size, quality, style, response_format, n)_get_langfuse_data_from_default_response()to parse image responses (URLs and base64 data wrapped inLangfuseMedia)_wrap()and_wrap_async()to track image count as usage metricstests/test_openai.pyimages.generate()images.edit()andimages.create_variation()(require DALL-E 2 + image files)Tracked Data
OpenAI-image(default)generate/edit) orNone(create_variation)dall-e-2,dall-e-3,gpt-image-1, etc.size,quality,style,response_format,nLangfuseMedia{output: N, total: N, unit: IMAGES})Notes
create_variation()does not accept a prompt parameter, soinputwill beNonefor those calls. This is expected behavior.LangfuseMediafor proper handling in the Langfuse UI.edit()andcreate_variation()are skipped as they require DALL-E 2 API access and PNG image files with alpha channels.Important
Adds support for tracking OpenAI image generation API calls in Langfuse, including handling image-specific parameters and responses.
langfuse/openai.py, includingimages.generate(),images.edit(), andimages.create_variation()._get_langfuse_data_from_kwargs()to handle image parameters likesize,quality,style,response_format, andn._get_langfuse_data_from_default_response()to parse image responses, wrapping base64 data inLangfuseMedia._wrap()and_wrap_async()to track image count as usage metrics.tests/test_openai.pyfor sync and asyncimages.generate().images.edit()andimages.create_variation()due to DALL-E 2 requirements.create_variation()does not use a prompt, soinputisNonefor these calls.This description was created by
for 7154a72. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Greptile Summary
This PR adds comprehensive tracking support for OpenAI image generation APIs (
images.generate(),images.edit(), andimages.create_variation()).Key changes:
OpenAiDefinitionentries for image methods (sync/async variants)LangfuseMediagenerate()with appropriate skipped stubs foredit()andcreate_variation()Implementation quality:
The implementation follows existing patterns in the codebase and handles edge cases well. The code properly differentiates between methods that accept prompts (
generate,edit) vs those that don't (create_variation). Response handling correctly unwraps single-image responses while preserving lists for multiple images. Usage tracking accurately counts images regardless of response format.Confidence Score: 4/5
Important Files Changed
Sequence Diagram
sequenceDiagram participant App as Application participant LF as Langfuse Wrapper participant OAI as OpenAI API participant DB as Langfuse Backend App->>LF: images.generate(prompt, model, size, ...) activate LF LF->>LF: _get_langfuse_data_from_kwargs() Note over LF: Extract prompt, model, params<br/>(size, quality, style, n) LF->>DB: start_observation(type="generation") LF->>OAI: images.generate() activate OAI OAI-->>LF: ImagesResponse (url or b64_json) deactivate OAI LF->>LF: _get_langfuse_data_from_default_response() Note over LF: Parse response, wrap base64 in<br/>LangfuseMedia, extract revised_prompt LF->>LF: Calculate usage (image_count) LF->>DB: update(output, usage_details, model) LF->>DB: end() LF-->>App: Return OpenAI response deactivate LF(5/5) You can turn off certain types of comments like style here!