Skip to content

Commit 4c58084

Browse files
committed
fix: revert to tool calling for structured output and handle unsupported models gracefully
1 parent 7b44c62 commit 4c58084

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

  • apps/python-service/routers

‎apps/python-service/routers/ai.py‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async def query_builder(request: QueryBuilderRequest, req: Request):
115115
)
116116

117117
# Enforce structured output based on our Pydantic schema
118-
structured_llm = llm.with_structured_output(QueryResult, method="json_mode")
118+
structured_llm = llm.with_structured_output(QueryResult)
119119

120120
# Build the system prompt
121121
system_prompt = """You are a highly intelligent database query builder for a MongoDB-based BaaS called urBackend.
@@ -168,6 +168,8 @@ async def query_builder(request: QueryBuilderRequest, req: Request):
168168
except Exception as e:
169169
error_name = e.__class__.__name__
170170
logger.error("[Trace: %s] ❌ AI Query Builder unhandled failure for developer_id=%s: %s", trace_id, request.developer_id, e, exc_info=True)
171+
if "Tool use is not supported" in str(e) or "tools" in str(e).lower():
172+
raise HTTPException(status_code=400, detail="This model doesn't support complex structured outputs. Please switch to llama-3.3-70b-versatile.") from e
171173
if "BadRequest" in error_name or "Validation" in error_name:
172174
raise HTTPException(status_code=400, detail="The AI generated an invalid query format. Please rephrase or simplify your prompt.") from e
173175
raise HTTPException(status_code=500, detail="Internal server error") from e
@@ -193,10 +195,9 @@ async def collection_creator(request: CollectionCreatorRequest, req: Request):
193195
model=request.model,
194196
)
195197

196-
structured_llm = llm.with_structured_output(CollectionCreatorResponse, method="json_mode")
198+
structured_llm = llm.with_structured_output(CollectionCreatorResponse)
197199

198200
system_prompt = """You are a MongoDB schema designer for urBackend, a Backend-as-a-Service platform.
199-
You MUST output valid JSON only.
200201
201202
Rules:
202203
1. If the description is vague, ask 2-3 specific clarifying questions and set type: "clarify". You MUST set "schema": null. NEVER ask more than 3 at once.
@@ -243,6 +244,8 @@ async def collection_creator(request: CollectionCreatorRequest, req: Request):
243244
except Exception as e:
244245
error_name = e.__class__.__name__
245246
logger.error("[Trace: %s] ❌ AI Collection Creator unhandled failure for developer_id=%s: %s", trace_id, request.developer_id, e, exc_info=True)
247+
if "Tool use is not supported" in str(e) or "tools" in str(e).lower():
248+
raise HTTPException(status_code=400, detail="This model doesn't support complex structured outputs. Please switch to llama-3.3-70b-versatile.") from e
246249
if "BadRequest" in error_name or "Validation" in error_name:
247250
raise HTTPException(status_code=400, detail="The AI generated an invalid response format. Please rephrase or simplify your prompt.") from e
248251
raise HTTPException(status_code=500, detail="Internal server error") from e

0 commit comments

Comments
 (0)