Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.
Python ai-agentsai-toolsbrowser-agentbrowser-automationbrowser-usecdpcloud-browsercomputer-useheadless-browserllmmcpplaywrightpythonremote-browserstealth-browserweb-automationweb-scraping
fix: add proper namespace support for Pydantic model_rebuild() with union types
Magnus Müllerbeae2fd20bdf
diff --git a/eval/service.py b/eval/service.pyindex e1240a886..84b800360 100644--- a/eval/service.py+++ b/eval/service.py@@ -1121,12 +1121,36 @@ def create_pydantic_model_from_schema(schema: dict, model_name: str = 'DynamicMomodule = importlib.util.module_from_spec(spec)+ # Add necessary imports to the module namespace before executing+ from typing import Any, Optional, Union++ from pydantic import BaseModel, Field++ module.__dict__.update(+ {+ 'Optional': Optional,+ 'Union': Union,+ 'list': list,+ 'dict': dict,+ 'Any': Any,+ 'BaseModel': BaseModel,+ 'Field': Field,+ 'str': str,+ 'int': int,+ 'float': float,+ 'bool': bool,+ }+ )+# Execute the generated code in the module's namespaceexec(generated_code, module.__dict__)# Get the generated model classif hasattr(module, model_name):model_class = getattr(module, model_name)+ # Rebuild the model to resolve forward references and type annotations+ # Pass the module's namespace so it can resolve imports like Optional+ model_class.model_rebuild(_types_namespace=module.__dict__)logger.debug(f'Successfully created Pydantic model: {model_class}')return model_classelse:@@ -1134,6 +1158,9 @@ def create_pydantic_model_from_schema(schema: dict, model_name: str = 'DynamicMofor attr_name in dir(module):attr = getattr(module, attr_name)if isinstance(attr, type) and issubclass(attr, BaseModel) and attr != BaseModel:+ # Rebuild the model to resolve forward references and type annotations+ # Pass the module's namespace so it can resolve imports like Optional+ attr.model_rebuild(_types_namespace=module.__dict__)logger.debug(f'Using fallback model class: {attr}')return attr@@ -1162,8 +1189,34 @@ def create_pydantic_model_from_schema(schema: dict, model_name: str = 'DynamicMofrom pydantic import create_model- def json_type_to_python_type(json_type: str):+ def json_type_to_python_type(json_type):"""Map JSON schema types to Python types"""+ # Handle union types (arrays of types)+ if isinstance(json_type, list):+ # Handle union types like ["string", "null"]+ types = []+ for t in json_type:+ if t == 'null':+ continue # We'll handle null separately+ types.append(json_type_to_python_type(t))++ if len(types) == 0:+ return Any+ elif len(types) == 1:+ return types[0]+ else:+ # len(types) >= 2 - create Union dynamically+ from typing import Union++ # For 2 types, use Union[type1, type2]+ if len(types) == 2:+ return Union[types[0], types[1]]+ # For more types, we need to use a different approach+ # Since Union doesn't support unpacking, we'll just use the first type+ # This is a limitation of the fallback implementation+ return types[0]++ # Handle single typesif json_type == 'string':return strelif json_type == 'integer':@@ -1191,10 +1244,14 @@ def create_pydantic_model_from_schema(schema: dict, model_name: str = 'DynamicMofield_definitions = {}for field_name, field_schema in properties.items():- field_type = json_type_to_python_type(field_schema.get('type'))+ json_type = field_schema.get('type')+ field_type = json_type_to_python_type(json_type)++ # Check if the field allows null (either not required or explicitly allows null)+ allows_null = field_name not in required_fields or (isinstance(json_type, list) and 'null' in json_type)# Handle required vs optional fields- if field_name in required_fields:+ if field_name in required_fields and not allows_null:field_definitions[field_name] = (field_type, ...) # Required fieldelse:optional_type = Optional[field_type] # Use Optional instead of Union[T, None]