Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

Remove required from prompt

Magnus Müller3aaafa7bf839
diff --git a/browser_use/llm/google/chat.py b/browser_use/llm/google/chat.py
index ad511bb26..3c11186a0 100644
--- a/browser_use/llm/google/chat.py
+++ b/browser_use/llm/google/chat.py
@@ -255,7 +255,7 @@ class ChatGoogle(BaseChatModel):
self.logger.debug(f'🔧 Requesting structured output for {output_format.__name__}')
config['response_mime_type'] = 'application/json'
# Convert Pydantic model to Gemini-compatible schema
- optimized_schema = SchemaOptimizer.create_optimized_json_schema(output_format)
+ optimized_schema = SchemaOptimizer.create_gemini_optimized_schema(output_format)
gemini_schema = self._fix_gemini_schema(optimized_schema)
config['response_schema'] = gemini_schema
diff --git a/browser_use/llm/schema.py b/browser_use/llm/schema.py
index da21a7be1..075564462 100644
--- a/browser_use/llm/schema.py
+++ b/browser_use/llm/schema.py
@@ -160,3 +160,31 @@ class SchemaOptimizer:
elif isinstance(schema, list):
for item in schema:
SchemaOptimizer._make_strict_compatible(item)
+
+ @staticmethod
+ def create_gemini_optimized_schema(model: type[BaseModel]) -> dict[str, Any]:
+ """
+ Create Gemini-optimized schema that removes 'required' arrays to save tokens.
+ Gemini can infer required fields from context since all fields are required.
+
+ Args:
+ model: The Pydantic model to optimize
+
+ Returns:
+ Optimized schema without required arrays
+ """
+ # Start with standard optimized schema
+ schema = SchemaOptimizer.create_optimized_json_schema(model)
+
+ def remove_required_arrays(obj: Any) -> Any:
+ """Recursively remove 'required' arrays"""
+ if isinstance(obj, dict):
+ # Remove 'required' key
+ result = {k: v for k, v in obj.items() if k != 'required'}
+ # Recursively process nested structures
+ return {k: remove_required_arrays(v) for k, v in result.items()}
+ elif isinstance(obj, list):
+ return [remove_required_arrays(item) for item in obj]
+ return obj
+
+ return remove_required_arrays(schema)