Skip to content

Commit cb35254

Browse files
authored
fix raw json schema support in extract (#215)
* fix raw json schema support in extract * formatting * add changeset
1 parent 6650346 commit cb35254

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"stagehand": patch
3+
---
4+
5+
Fix ability to pass raw JSON to Extract schema

stagehand/handlers/extract_handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ async def extract(
151151

152152
processed_data_payload = raw_data_dict # Default to the raw dictionary
153153

154-
if schema and isinstance(
155-
raw_data_dict, dict
156-
): # schema is the Pydantic model type
154+
if schema and isinstance(schema, type) and issubclass(schema, BaseModel):
157155
# Try direct validation first
158156
try:
159157
validated_model_instance = schema.model_validate(raw_data_dict)

stagehand/llm/inference.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,19 @@ async def extract(
169169
start_time = time.time()
170170

171171
# Determine if we need to use schema-based response format
172-
# TODO: if schema is json, return json
173172
response_format = {"type": "json_object"}
174173
if schema:
175-
# If schema is a Pydantic model, use it directly
176-
response_format = schema
174+
if isinstance(schema, dict):
175+
response_format = {
176+
"type": "json_schema",
177+
"json_schema": {
178+
"name": "extraction_schema",
179+
"strict": False,
180+
"schema": schema,
181+
},
182+
}
183+
else:
184+
response_format = schema
177185

178186
# Call the LLM with appropriate parameters
179187
try:

0 commit comments

Comments
 (0)