3737 UserPromptPart ,
3838)
3939from pydantic_ai ._json_schema import InlineDefsJsonSchemaTransformer
40- from pydantic_ai .builtin_tools import WebSearchTool
40+ from pydantic_ai .builtin_tools import ImageGenerationTool , WebSearchTool
4141from pydantic_ai .models import ModelRequestParameters
4242from pydantic_ai .output import NativeOutput , PromptedOutput , TextOutput , ToolOutput
4343from pydantic_ai .profiles .openai import OpenAIModelProfile , openai_model_profile
7979 OpenAIResponsesModel ,
8080 OpenAIResponsesModelSettings ,
8181 OpenAISystemPromptRole ,
82+ _resolve_openai_image_generation_size , # pyright: ignore[reportPrivateUsage]
8283 )
8384 from pydantic_ai .profiles .openai import OpenAIJsonSchemaTransformer
8485 from pydantic_ai .providers .cerebras import CerebrasProvider
@@ -103,6 +104,43 @@ def test_init():
103104 assert m .model_name == 'gpt-4o'
104105
105106
107+ @pytest .mark .parametrize (
108+ 'aspect_ratio,size,expected' ,
109+ [
110+ # aspect_ratio is None, various sizes
111+ (None , None , 'auto' ),
112+ (None , 'auto' , 'auto' ),
113+ (None , '1024x1024' , '1024x1024' ),
114+ (None , '1024x1536' , '1024x1536' ),
115+ (None , '1536x1024' , '1536x1024' ),
116+ # Valid aspect_ratios with no size
117+ ('1:1' , None , '1024x1024' ),
118+ ('2:3' , None , '1024x1536' ),
119+ ('3:2' , None , '1536x1024' ),
120+ # Valid aspect_ratios with compatible sizes
121+ ('1:1' , 'auto' , '1024x1024' ),
122+ ('1:1' , '1024x1024' , '1024x1024' ),
123+ ('2:3' , '1024x1536' , '1024x1536' ),
124+ ('3:2' , '1536x1024' , '1536x1024' ),
125+ ],
126+ )
127+ def test_openai_image_generation_size_valid_combinations (
128+ aspect_ratio : Literal ['1:1' , '2:3' , '3:2' ] | None ,
129+ size : Literal ['auto' , '1024x1024' , '1024x1536' , '1536x1024' ] | None ,
130+ expected : Literal ['auto' , '1024x1024' , '1024x1536' , '1536x1024' ],
131+ ) -> None :
132+ """Test valid combinations of aspect_ratio and size for OpenAI image generation."""
133+ tool = ImageGenerationTool (aspect_ratio = aspect_ratio , size = size )
134+ assert _resolve_openai_image_generation_size (tool ) == expected
135+
136+
137+ def test_openai_image_generation_tool_aspect_ratio_invalid () -> None :
138+ """Test that invalid aspect_ratio raises UserError."""
139+ tool = ImageGenerationTool (aspect_ratio = '16:9' )
140+ with pytest .raises (UserError , match = 'OpenAI image generation only supports `aspect_ratio` values' ):
141+ _resolve_openai_image_generation_size (tool )
142+
143+
106144async def test_request_simple_success (allow_model_requests : None ):
107145 c = completion_message (
108146 ChatCompletionMessage (content = 'world' , role = 'assistant' ),
0 commit comments