8000 Add AI task structured output by allenporter · Pull Request #148083 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add AI task structured output #148083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

allenporter
Copy link
Contributor

Proposed change

Add AI Task structured output support. The generate data service accepts an input schema and APIs to allow an AI task entity to generate a response with an object conforming to that structure. Integrations are responsible for converting the schema (based on selectors) to the appropriate type for the API (e.g. json schema)

Architecture discussion: home-assistant/architecture#1216 - has been approved by core team according to balloob, but not yet marked as approved.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@Copilot Copilot AI review requested due to automatic review settings July 3, 2025 21:30
@allenporter allenporter requested a review from a team as a code owner July 3, 2025 21:30
@allenporter allenporter marked this pull request as draft July 3, 2025 21:30
@home-assistant
Copy link
home-assistant bot commented Jul 3, 2025

Hey there @home-assistant/core, mind taking a look at this pull request as it has been labeled with an integration (ai_task) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of ai_task can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign ai_task Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds optional structured output support to the AI Task integration, enabling callers to supply a schema for the AI to generate data in a typed, field-based format.

  • Introduces a new structure parameter in async_generate_data, guarded by a new GENERATE_STRUCTURED_DATA entity feature
  • Validates user‐supplied structure definitions via a new _validate_structure helper and STRUCTURE_FIELD_SCHEMA
  • Extends tests and mocks to cover structured‐output generation and invalid‐schema handling

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/components/ai_task/test_task.py Added tests for unsupported structured‐output cases
tests/components/ai_task/test_init.py Added tests for valid and invalid structure schemas
tests/components/ai_task/test_entity.py Added structured‐output test on entity API
tests/components/ai_task/conftest.py Updated mock to advertise and handle structured data
homeassistant/helpers/service.py Registered ai_task in base component lookup
homeassistant/components/ai_task/task.py Added structure param and feature check in API
homeassistant/components/ai_task/const.py Defined ATTR_STRUCTURE, ATTR_REQUIRED, and new feature flag
homeassistant/components/ai_task/init.py Added service schema and _validate_structure
homeassistant/components/ai_task/services.yaml Declared structure parameter for generate_data
homeassistant/components/ai_task/strings.json Added localization for the structure parameter
Comments suppressed due to low confidence (2)

homeassistant/components/ai_task/services.yaml:23

  • The example value is not valid JSON and has mismatched braces. Update it to a valid JSON string, e.g.: example: '{"name": {"selector": {"text": {}}, "description": "Name of the user", "required": true}, "age": {"selector": {"number": {}}, "description": "Age of the user"}}'.
      example: '{ "name": { "selector": { "text": }, "description": "Name of the user", "required": "True" } } }, "age": { "selector": { "number": }, "description": "Age of the user" } }'

homeassistant/components/ai_task/services.yaml:24

  • The object: selector is specified without an explicit mapping. It should be object: {} to be valid YAML.
      selector:

@allenporter allenporter marked this pull request as ready for review July 4, 2025 00:36
@@ -32,3 +34,6 @@ class AITaskEntityFeature(IntFlag):

GENERATE_DATA = 1
"""Generate data based on instructions."""

GENERATE_STRUCTURED_DATA = 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for an extra supported feature. It's folded into GENERATE_DATA and we won't allow AI Task entities that cannot do that.

init_components: None,
mock_ai_task_entity: MockAITaskEntity,
) -> None:
"""Test the entity can generate structured data with a top level object schemea."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""Test the entity can generate structured data with a top level object schemea."""
"""Test the entity can generate structured data with a top level object schema."""

Comment on lines +131 to +151


async def test_run_task_structure_unsupported_feature(
hass: HomeAssistant,
init_components: None,
mock_ai_task_entity: MockAITaskEntity,
) -> None:
"""Test running a task with an unknown entity."""

mock_ai_task_entity.supported_features = AITaskEntityFeature.GENERATE_DATA
with pytest.raises(
HomeAssistantError,
match="AI Task entity ai_task.test_task_entity does not support generating structured data",
):
await async_generate_data(
hass,
task_name="Test Task",
instructions="Test prompt",
entity_id=TEST_ENTITY_ID,
structure=vol.Schema({vol.Required("name"): selector.TextSelector()}),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove this one

Suggested change
async def test_run_task_structure_unsupported_feature(
hass: HomeAssistant,
init_components: None,
mock_ai_task_entity: MockAITaskEntity,
) -> None:
"""Test running a task with an unknown entity."""
mock_ai_task_entity.supported_features = AITaskEntityFeature.GENERATE_DATA
with pytest.raises(
HomeAssistantError,
match="AI Task entity ai_task.test_task_entity does not support generating structured data",
):
await async_generate_data(
hass,
task_name="Test Task",
instructions="Test prompt",
entity_id=TEST_ENTITY_ID,
structure=vol.Schema({vol.Required("name"): selector.TextSelector()}),
)

Copy link
Member
@balloob balloob left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok to merge once the supported feature is removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to co 7220 mment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0