Skip to content

LCORE-4717: map text.format to the agent output type - #3355

Open
max-svistunov wants to merge 3 commits into
lightspeed-core:mainfrom
max-svistunov:lcore-4717-text-format-output-type
Open

max-svistunov wants to merge 3 commits into
lightspeed-core:mainfrom
max-svistunov:lcore-4717-text-format-output-type

Conversation

@max-svistunov

@max-svistunov max-svistunov commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Description

LCORE-4717: OutputSettings, the per-request agent setting that maps text.format of a /v1/responses request to the agent's output_type (design pai-responses-lcore-rewrite.md, section 4.8). It follows the shape of the other settings in src/agent/types/settings/ (#3204): a dataclass with a from_responses_request constructor, so the Responses input adapter (LCORE-4718) builds it the same way as the rest. Two commits:

  1. OutputSettings.from_responses_request(params) and output_type_for(profile): type text or no format is str; json_object is a StructuredDict over a free-form object; json_schema is a StructuredDict over the client's schema with its name and description. The structured type is wrapped in the output mode the selected model's profile can honor: native when the model supports JSON schema output, tool-based when it supports tools, prompted otherwise; strict reaches the native and tool wrappers. A json_schema format without a schema or a name, or whose schema does not describe an object, is a 422 raised when the settings are built, before any model call. The format type itself is validated by the request model. text.verbosity is not part of the output type.
  2. When the request declares client function tools, DeferredToolRequests joins the output type so a function call can end the run; server-side tools (MCP, file search) run inside the loop and do not. AgentSettings gains an output field, plain text for a query.

The agent factory does not read the field yet; passing the output type to the agent is one line there once the adapter (LCORE-4718) fills it. No API or configuration change.

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library [pyproject.toml + uv.lock]
  • Bump-up dependent library [requirements.*.txt for Konflux]
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement
  • Benchmarks improvement

Tools used to create PR

Identify any AI code assistants used in this PR (for transparency and review context)

  • Assisted-by: Claude Opus 4.8
  • Generated by: Claude Opus 4.8

Related Tickets & Documents

  • Related Issue # LCORE-4717
  • Closes # LCORE-4717

Checklist before requesting a review

  • I have performed a self-review of my code.
  • PR has passed all pre-merge test jobs.
  • If it is a core feature, I have added thorough tests.

Testing

Run the mapper's tests:

uv run pytest tests/unit/agent/types/settings/test_output.py -v

20 tests: text and absent formats, the free-form json_object schema, a client schema with name and description, strict, the three output modes by profile and the fallback for an unknown profile, four incomplete or invalid schemas rejected with 422 and a cause naming what is missing, and the deferred-tools union (text and structured, server tools excluded, and a pydantic-ai Agent accepting the produced union), and the plain-text default on AgentSettings.

ruff, black, pydocstyle, mypy, pyright and pylint (10.00) are clean on the touched files; the settings tests of #3204 still pass. No stack run: the function has no caller yet.

Summary by CodeRabbit

  • New Features
    • Responses can now be configured to return plain text, JSON objects, or JSON schema-constrained output.
    • JSON schema responses support a name, description, and strict-mode setting. Incomplete or invalid schemas are rejected.
    • Output is handled using the method best suited to the selected model, including requests that use client-executed function tools.
    • When no output format is specified, responses default to plain text.

text.format is the output contract of a /v1/responses request. On the
pydantic-ai path the agent enforces it through output_type, so add
OutputSettings next to the other per-request agent settings (design,
section 4.8). from_responses_request reads the format: type text or no
format is plain text; json_object is a free-form object; json_schema is
the client's schema with its name and description. output_type_for wraps
the structured type in the output mode the selected model's profile can
honor: native when the model supports JSON schema output, tool-based when
it supports tools, prompted otherwise; strict reaches the native and tool
wrappers.

A json_schema format without a schema or a name, or whose schema does not
describe an object, is a 422 raised when the settings are built, before
any model call. The format type itself is validated by the request model.
text.verbosity is not part of the output type.

Deferred client function calls join the output type in the next commit.
…ction tools

A function tool the client executes itself ends the agent run with a call
the client has to answer. pydantic-ai models that as DeferredToolRequests
in the output type, so when the request declares function tools
OutputSettings records it and output_type_for returns the union of the
contract (str or the structured type in its output mode) and
DeferredToolRequests. Server-side tools, MCP and file search, run inside
the loop and do not change the output type.

AgentSettings gains an output field, plain text for a query, so the agent
factory can pass the output type to the agent once the Responses adapter
builds the settings from a request.
@coderabbitai

coderabbitai Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds OutputSettings to parse Responses text.format, validate JSON schemas, and select output contracts based on model capabilities. AgentSettings now includes default output settings, and the settings package exports OutputSettings.

Changes

Responses output settings

Layer / File(s) Summary
Parse formats and select output contracts
src/agent/types/settings/output.py, tests/unit/agent/types/settings/test_output.py
OutputSettings parses text formats, validates json_schema requests, and builds structured output contracts. It selects native, tool, or prompted output based on model capabilities. Client function tools add DeferredToolRequests. Tests cover defaults, validation, contract selection, and tool handling.
Expose output settings through AgentSettings
src/agent/types/settings/agent.py, src/agent/types/settings/__init__.py, tests/unit/agent/types/settings/test_output.py
AgentSettings now has a default output field, and the settings package exports OutputSettings. Tests check that Agent accepts the resulting output type and that default settings use plain-text output.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant ResponsesRequest
  participant OutputSettings
  participant ModelProfile
  ResponsesRequest->>OutputSettings: parse text.format and validate schema
  OutputSettings->>ModelProfile: select structured output contract
  ModelProfile-->>OutputSettings: provide model capabilities
Loading

Possibly related PRs

Suggested reviewers: jrobertboos, tisnik, asimurka

Merge Risk: Minimal · up to be273

This adds output settings that nothing uses yet, so current behavior does not change. The new logic is covered by unit tests. Merge risk is minimal.

Pre-merge checks: Passed 6

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the primary change: mapping the /v1/responses text.format setting to the agent output type. The raw summary confirms the new OutputSettings class, its from_responses_request mapping, a…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Performance And Algorithmic Complexity Passed The PR adds a per-request settings mapper and does not introduce a performance regression under the five listed criteria. Changed files (from git diff --name-status): `src/agent/types/settings/__ini…
Security And Secret Handling Passed Security and Secret Handling check: no violation found in the PR's changed code. Scope reviewed (base..head diff): src/agent/types/settings/__init__.py, src/agent/types/settings/agent.py, new `src…
Full details: Title check

Explanation

The title names the primary change: mapping the /v1/responses text.format setting to the agent output type. The raw summary confirms the new OutputSettings class, its from_responses_request mapping, and the AgentSettings output field. The title is specific and accurate, and it carries the ticket ID, which is acceptable.

Full details: Performance And Algorithmic Complexity

Explanation

The PR adds a per-request settings mapper and does not introduce a performance regression under the five listed criteria. Changed files (from git diff --name-status): src/agent/types/settings/__init__.py (export), src/agent/types/settings/agent.py (new output field), src/agent/types/settings/output.py (new OutputSettings), and a new unit test file. Checks against the criteria: 1. O(n²)+ work: None. from_responses_request does constant-time key lookups and an any() over params.tools, which is linear in the request's tool count. The schema check is one isinstance and get call. 2. N+1 patterns: None. No API, DB, or K8s calls. 3. Expensive work in loops: None. There are no loops over large inputs. StructuredDict and the output-mode wrapper are built once per request. The client schema is passed through without re-parsing. 4. Unbounded growth: None. The class adds no cache, buffer, or watcher. 5. Pagination or limits: Not applicable. The PR adds no List operation or endpoint. The agent factory does not read the new field yet, so no runtime path changes. The only per-request cost is building a pydantic-ai wrapper and validating the client's JSON schema. Both are bounded by request size and run once per request, so I found no issue to report.

Full details: Security And Secret Handling

Explanation

Security and Secret Handling check: no violation found in the PR's changed code. Scope reviewed (base..head diff): src/agent/types/settings/__init__.py, src/agent/types/settings/agent.py, new src/agent/types/settings/output.py, and new tests/unit/agent/types/settings/test_output.py. The change adds an internal settings mapper. It adds no endpoint, route, or handler. Per criterion: 1. Secrets/tokens: No hardcoded credentials, keys, or tokens in the diff. Grep for token, secret, password, api_key, and bearer found no new hits in tests/ or src/. The only match in src/ is a pre-existing docstring line about MCP auth headers in agent.py, unchanged in substance. 2. Auth/authz: No API endpoints are added or modified, so the endpoint auth criterion does not apply. The PR states the agent factory does not yet read the new field. 3. Injection: The client-supplied text.format schema is passed as a JSON schema object into pydantic-ai StructuredDict, NativeOutput, ToolOutput, or PromptedOutput. No SQL, shell, filesystem, or eval/exec use appears in the diff. The json_schema checks are type checks that raise 422. 4. Sensitive data leakage: The 422 errors use fixed cause strings (for example, "json_schema requires a name"). They do not echo the schema or request content. logger is created in output.py but not called in the diff, so nothing sensitive is logged. 5. K8s/RH secrets and OwnerReferences: No Kubernetes or Red Hat secret manifests are in the changed files. Limits: The check reads only the four changed files. The PR does not change request routing, so auth on the wider /v1/responses path is unchanged and was not re-verified here. No finding relates to that unchanged path.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Simplify code

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 249b3bf and be27307.

Files selected for processing: 2

  • src/agent/types/settings/output.py
  • tests/unit/agent/types/settings/test_output.py

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Recent review details

Context from checks skipped due to timeout: 20

  • GitHub Check: E2E: ci / shields
  • GitHub Check: E2E: ci / skills
  • GitHub Check: E2E: ci / authorized
  • GitHub Check: E2E: ci / default
  • GitHub Check: E2E: ci / mcp
  • GitHub Check: E2E: ci / other
  • GitHub Check: E2E: ci / rbac
  • GitHub Check: unit_tests (3.14)
  • GitHub Check: mypy
  • GitHub Check: unit_tests (3.12)
  • GitHub Check: integration_tests (3.14)
  • GitHub Check: integration_tests (3.12)
  • GitHub Check: build-pr
  • GitHub Check: unit_tests (3.13)
  • GitHub Check: Pylinter
  • GitHub Check: integration_tests (3.13)
  • GitHub Check: Red Hat Konflux / lightspeed-core-0-8-enterprise-contract / lightspeed-stack-0-8
  • GitHub Check: Red Hat Konflux / rag-content-0-8-e2e-tests / lightspeed-stack-0-8
  • GitHub Check: Red Hat Konflux / lightspeed-stack-0-8-e2e-tests / lightspeed-stack-0-8
  • GitHub Check: Konflux kflux-prd-rh02 / lightspeed-stack-0-8-on-pull-request
Additional context used

Code guidelines: 2

AGENTS.md — auto-discovered
CLAUDE.md — auto-discovered
Path-based instructions: 4

Source excerpt: **Required**: Use pytest for all unit and integration tests Source excerpt: **Forbidden**: Do not use unittest - pytest is the standard for this project Source excerpt: **Coverage**: Unit tests require 60% coverage, integrat...

CodeRabbit inference engine (AGENTS.md)

Files:

  • tests/unit/agent/types/settings/test_output.py
Source excerpt: **Required**: Use pytest for all unit and integration tests Source excerpt: **Forbidden**: Do not use unittest - pytest is the standard for this project Source excerpt: **Mocking**: `pytest-mock` for AsyncMock objects

CodeRabbit inference engine (CLAUDE.md)

Files:

  • tests/unit/agent/types/settings/test_output.py
Source excerpt: **Coverage**: Unit tests require 60% coverage, integration 10% Source excerpt: **Async tests**: Use marker `pytest.mark.asyncio`

CodeRabbit inference engine (CLAUDE.md)

Files:

  • tests/unit/agent/types/settings/test_output.py
Source excerpt: **Mocking**: `pytest-mock` for AsyncMock objects Source excerpt: **Async tests**: Use marker `pytest.mark.asyncio`

CodeRabbit inference engine (AGENTS.md)

Files:

  • tests/unit/agent/types/settings/test_output.py
Learnings: 1

Learning: 2026-11-17T11:23:03.910Z

Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-11-17T11:23:03.910Z
Learning: Source excerpt:
# Lightspeed Core Stack Development Guide

## Quality Assurance

### Required Before Completion

3. Create unit tests for new code

Applied to files:

  • src/agent/types/settings/output.py

Comment thread src/agent/types/settings/output.py Fixed
…xplicitly

Code scanning flagged the assert that guarded the schema of a json_schema
format: an assert is dropped under optimized byte code and is not a
contract. from_responses_request never builds such settings, but settings
assembled by hand can; they now get the same 422 the request path gives.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants