feat: Implement structured output for AI agent Tasks#53285
Open
ryans-posthog wants to merge 15 commits intomasterfrom
Open
feat: Implement structured output for AI agent Tasks#53285ryans-posthog wants to merge 15 commits intomasterfrom
ryans-posthog wants to merge 15 commits intomasterfrom
Conversation
Contributor
|
Size Change: 0 B Total Size: 127 MB ℹ️ View Unchanged
|
Contributor
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>
ryans-posthog
commented
Apr 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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