Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/fortunate-friendly-avocet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stagehand": patch
---

Fix ability to pass raw JSON to Extract schema
4 changes: 1 addition & 3 deletions stagehand/handlers/extract_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ async def extract(

processed_data_payload = raw_data_dict # Default to the raw dictionary

if schema and isinstance(
raw_data_dict, dict
): # schema is the Pydantic model type
if schema and isinstance(schema, type) and issubclass(schema, BaseModel):
# Try direct validation first
try:
validated_model_instance = schema.model_validate(raw_data_dict)
Expand Down
14 changes: 11 additions & 3 deletions stagehand/llm/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,19 @@ async def extract(
start_time = time.time()

# Determine if we need to use schema-based response format
# TODO: if schema is json, return json
response_format = {"type": "json_object"}
if schema:
# If schema is a Pydantic model, use it directly
response_format = schema
if isinstance(schema, dict):
response_format = {
"type": "json_schema",
"json_schema": {
"name": "extraction_schema",
"strict": False,
"schema": schema,
},
}
else:
response_format = schema

# Call the LLM with appropriate parameters
try:
Expand Down