|
1 |
| -import json |
2 | 1 | import os
|
3 | 2 | import sys
|
4 |
| -from unittest.mock import AsyncMock, MagicMock, patch |
| 3 | +from typing import List |
5 | 4 |
|
6 |
| -import httpx |
7 |
| -import pytest |
| 5 | +from pydantic import BaseModel |
8 | 6 |
|
9 | 7 | sys.path.insert(
|
10 | 8 | 0, os.path.abspath("../../../../..")
|
@@ -66,3 +64,159 @@ def test_get_model_name_from_gemini_spec_model():
|
66 | 64 | model = "gemini/ft-uuid-123"
|
67 | 65 | result = VertexGeminiConfig._get_model_name_from_gemini_spec_model(model)
|
68 | 66 | assert result == "ft-uuid-123"
|
| 67 | + |
| 68 | + |
| 69 | +def test_vertex_ai_response_schema_dict(): |
| 70 | + v = VertexGeminiConfig() |
| 71 | + transformed_request = v.map_openai_params( |
| 72 | + non_default_params={ |
| 73 | + "messages": [{"role": "user", "content": "Hello, world!"}], |
| 74 | + "response_format": { |
| 75 | + "type": "json_schema", |
| 76 | + "json_schema": { |
| 77 | + "name": "math_reasoning", |
| 78 | + "schema": { |
| 79 | + "type": "object", |
| 80 | + "properties": { |
| 81 | + "steps": { |
| 82 | + "type": "array", |
| 83 | + "items": { |
| 84 | + "type": "object", |
| 85 | + "properties": { |
| 86 | + "thought": {"type": "string"}, |
| 87 | + "output": {"type": "string"}, |
| 88 | + }, |
| 89 | + "required": ["thought", "output"], |
| 90 | + "additionalProperties": False, |
| 91 | + }, |
| 92 | + }, |
| 93 | + "final_answer": {"type": "string"}, |
| 94 | + }, |
| 95 | + "required": ["steps", "final_answer"], |
| 96 | + "additionalProperties": False, |
| 97 | + }, |
| 98 | + "strict": False, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + optional_params={}, |
| 103 | + model="gemini-2.0-flash-lite", |
| 104 | + drop_params=False, |
| 105 | + ) |
| 106 | + |
| 107 | + schema = transformed_request["response_schema"] |
| 108 | + # should add propertyOrdering |
| 109 | + assert schema["propertyOrdering"] == ["steps", "final_answer"] |
| 110 | + # should add propertyOrdering (recursively, including array items) |
| 111 | + assert schema["properties"]["steps"]["items"]["propertyOrdering"] == [ |
| 112 | + "thought", |
| 113 | + "output", |
| 114 | + ] |
| 115 | + # should strip strict and additionalProperties |
| 116 | + assert "strict" not in schema |
| 117 | + assert "additionalProperties" not in schema |
| 118 | + # validate the whole thing to catch regressions |
| 119 | + assert transformed_request["response_schema"] == { |
| 120 | + "type": "object", |
| 121 | + "properties": { |
| 122 | + "steps": { |
| 123 | + "type": "array", |
| 124 | + "items": { |
| 125 | + "type": "object", |
| 126 | + "properties": { |
| 127 | + "thought": {"type": "string"}, |
| 128 | + "output": {"type": "string"}, |
| 129 | + }, |
| 130 | + "required": ["thought", "output"], |
| 131 | + "propertyOrdering": ["thought", "output"], |
| 132 | + }, |
| 133 | + }, |
| 134 | + "final_answer": {"type": "string"}, |
| 135 | + }, |
| 136 | + "required": ["steps", "final_answer"], |
| 137 | + "propertyOrdering": ["steps", "final_answer"], |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | +class MathReasoning(BaseModel): |
| 142 | + steps: List["Step"] |
| 143 | + final_answer: str |
| 144 | + |
| 145 | + |
| 146 | +class Step(BaseModel): |
| 147 | + thought: str |
| 148 | + output: str |
| 149 | + |
| 150 | + |
| 151 | +def test_vertex_ai_response_schema_defs(): |
| 152 | + v = VertexGeminiConfig() |
| 153 | + |
| 154 | + schema = v.get_json_schema_from_pydantic_object(MathReasoning) |
| 155 | + |
| 156 | + # pydantic conversion by default adds $defs to the schema, make sure this is still the case, otherwise this test isn't really testing anything |
| 157 | + assert "$defs" in schema["json_schema"]["schema"] |
| 158 | + |
| 159 | + transformed_request = v.map_openai_params( |
| 160 | + non_default_params={ |
| 161 | + "messages": [{"role": "user", "content": "Hello, world!"}], |
| 162 | + "response_format": schema, |
| 163 | + }, |
| 164 | + optional_params={}, |
| 165 | + model="gemini-2.0-flash-lite", |
| 166 | + drop_params=False, |
| 167 | + ) |
| 168 | + |
| 169 | + assert "$defs" not in transformed_request["response_schema"] |
| 170 | + assert transformed_request["response_schema"] == { |
| 171 | + "title": "MathReasoning", |
| 172 | + "type": "object", |
| 173 | + "properties": { |
| 174 | + "steps": { |
| 175 | + "title": "Steps", |
| 176 | + "type": "array", |
| 177 | + "items": { |
| 178 | + "title": "Step", |
| 179 | + "type": "object", |
| 180 | + "properties": { |
| 181 | + "thought": {"title": "Thought", "type": "string"}, |
| 182 | + "output": {"title": "Output", "type": "string"}, |
| 183 | + }, |
| 184 | + "required": ["thought", "output"], |
| 185 | + "propertyOrdering": ["thought", "output"], |
| 186 | + }, |
| 187 | + }, |
| 188 | + "final_answer": {"title": "Final Answer", "type": "string"}, |
| 189 | + }, |
| 190 | + "required": ["steps", "final_answer"], |
| 191 | + "propertyOrdering": ["steps", "final_answer"], |
| 192 | + } |
| 193 | + |
| 194 | + |
| 195 | +def test_vertex_ai_retain_property_ordering(): |
| 196 | + v = VertexGeminiConfig() |
| 197 | + transformed_request = v.map_openai_params( |
| 198 | + non_default_params={ |
| 199 | + "messages": [{"role": "user", "content": "Hello, world!"}], |
| 200 | + "response_format": { |
| 201 | + "type": "json_schema", |
| 202 | + "json_schema": { |
| 203 | + "name": "math_reasoning", |
| 204 | + "schema": { |
| 205 | + "type": "object", |
| 206 | + "properties": { |
| 207 | + "output": {"type": "string"}, |
| 208 | + "thought": {"type": "string"}, |
| 209 | + }, |
| 210 | + "propertyOrdering": ["thought", "output"], |
| 211 | + }, |
| 212 | + }, |
| 213 | + }, |
| 214 | + }, |
| 215 | + optional_params={}, |
| 216 | + model="gemini-2.0-flash-lite", |
| 217 | + drop_params=False, |
| 218 | + ) |
| 219 | + |
| 220 | + schema = transformed_request["response_schema"] |
| 221 | + # should leave existing value alone, despite dictionary ordering |
| 222 | + assert schema["propertyOrdering"] == ["thought", "output"] |
0 commit comments