feat: Implement structured output for AI agent Tasks#53285
Conversation
|
Size Change: -12.2 kB (-0.01%) Total Size: 129 MB
ℹ️ View Unchanged
|
Prompt To Fix All With AIThis is a comment left during a code review.
Path: products/tasks/backend/models.py
Line: 861
Comment:
**Wrong field: `state` vs `status`**
`instance.state` is a `JSONField` that stores run execution data (e.g., `{"mode": "background"}`), not the run's completion status. Comparing it to `TaskRun.Status.COMPLETED` (a string like `"completed"`) will always evaluate to `False`, so `track_structured_result` is never invoked from this signal — the analytics event is silently dropped every time.
The correct field is `instance.status`:
```suggestion
if not created and instance.status == TaskRun.Status.COMPLETED and instance.output and instance.task.json_schema:
```
How can I resolve this? If you propose a fix, please make it concise.
---
This is a comment left during a code review.
Path: products/tasks/backend/models.py
Line: 534-546
Comment:
**Redundant nested try/except with duplicate log key**
`track_structured_result` already wraps `capture_event` in its own `try/except` that catches all exceptions and logs them under `"task_run.track_structured_result_failed"`. The outer `try/except` in `track_task_run_completion` uses the exact same log key. Because the inner handler swallows everything, the outer one can only ever fire for errors in the condition check (e.g., `instance.task.json_schema` raising), which is a different concern that would be clearer with its own log message. Consider consolidating to a single layer of error handling.
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "Merge branch 'master' into ryan/implemen..." | Re-trigger Greptile |
Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
| ) | ||
| task_run.output = output_data | ||
| task_run.save(update_fields=["output", "updated_at"]) | ||
| self._signal_workflow_completion(task_run, TaskRun.Status.COMPLETED, None) |
There was a problem hiding this comment.
can't remember if we store pr_url on output or on state right now, i think it's on output, in which case we currently don't have a model of "output is set only once" - worth checking that here, as we don't want to mark coding wokrflows as completed ever.
if we are doing that, i'd drop this part for now and let folks poll for output
There was a problem hiding this comment.
perhaps we complete if and only if it the task has json schema?
There was a problem hiding this comment.
Ahhh that's a good point, I think I had that specific condition on something else and forgot to add it here.
Problem
Currently, there is no way to specify the shape of the output for a task. The tasks are currently expected to either return human language responses, or be executed interactively via PostHog Code.
Changes
This change introduces validation on the set_output endpoint and transfer of the
json_schemato the code agent.How did you test this code?
Unit testing for setting the json schema and the action of the
set_outputendpoint.Manual testing:
Checkout PostHog/code#1466 on the Code repo and recompile the agent. Clear the docker image for the sandbox runners.
Create a new task via
python manage.py shellwith theTask.create_and_runfunction. Make sure to set the json_schema in the task. Verify that the Task executes and uses theset_outputendpoint.👉 Stay up-to-date with PostHog coding conventions for a smoother review.
Publish to changelog?
Docs update