Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def main():
# Extract companies using structured schema
companies_data = await page.extract(
"Extract names and descriptions of 5 companies in batch 3",
schema=Companies
schema_definition=Companies
)

# Display results
Expand All @@ -66,4 +66,4 @@ async def main():
await stagehand.close()

if __name__ == "__main__":
asyncio.run(main())
asyncio.run(main())
26 changes: 26 additions & 0 deletions stagehand/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ def _resolve_references(self, obj: Any, definitions: dict, ref_prefix: str) -> N
model_config = ConfigDict(arbitrary_types_allowed=True)


class AttrDict(dict):
"""A dictionary that allows attribute-style access to its items."""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__dict__ = self


class ExtractResult(StagehandBaseModel):
"""
Result of the 'extract' command.
Expand All @@ -171,6 +179,24 @@ class ExtractResult(StagehandBaseModel):

model_config = ConfigDict(extra="allow") # Allow any extra fields

def __init__(self, **data):
"""Initialize and recursively convert nested dictionaries to AttrDict objects."""
# Convert nested dictionaries to AttrDict for attribute access
converted_data = self._convert_to_attr_dict(data)
super().__init__(**converted_data)

def _convert_to_attr_dict(self, obj):
"""Recursively convert dictionaries to AttrDict objects."""
if isinstance(obj, dict):
# Convert dict to AttrDict and recursively convert nested objects
attr_dict = AttrDict()
for key, value in obj.items():
attr_dict[key] = self._convert_to_attr_dict(value)
return attr_dict
elif isinstance(obj, list):
return [self._convert_to_attr_dict(item) for item in obj]
return obj

def __getitem__(self, key):
"""
Enable dictionary-style access to attributes.
Expand Down