sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[FEATURE] Support TypeAdapter-compatible types in `structured_output`

Open rnapier opened this issue 6 months ago • 2 comments

Problem Statement

I would like the AI to return a list of results. In Pydantic, this is done with TypeAdapter:

TypeAdapter(list[Link]).validate_json(output)

I would like convert this to structured_output. Ideally directly, without needing TypeAdapter (having Strands insert it):

agent.structured_output(list[Link], " ... ")

But it would also be fine to require TypeAdapter:

agent.structured_output(TypeAdapter(list[Link]), " ... ")

Proposed Solution

No response

Use Case

I currently have a prompt that instructs the model to generate a JSON list of JSON objects. Other solutions that don't require a wrapping JSON list would be fine as well.

Alternatives Solutions

No response

Additional Context

No response

rnapier avatar Jul 23 '25 13:07 rnapier

Hello @mkmeral , I'd like to contribute to this.

Ratish1 avatar Dec 11 '25 13:12 Ratish1

Hi @mkmeral, I have questions about supporting list[Link] / TypeAdapter in structured_output:

  1. LLM providers need tool inputSchema to be a JSON object. Should Strands automatically wrap array/top-level non-object schemas (e.g. list[Link] -> {"response": list[Link]})?

  2. For agent.structured_output(list[Link]) do you prefer: A) create a dynamic BaseModel wrapper (response: list[Link]) for schema generation and reuse BaseModel logic, or B) accept TypeAdapter internally and wrap its json_schema() if needed?

  3. Should StructuredOutputTool accept Any (TypeAdapter, typing constructs, BaseModel) and normalize internally, or require callers to pass TypeAdapter/BaseModel before initialization?

  4. For caching, should we use adapter.annotation as cache key or fallback to repr(annotation) as implemented?

Thanks.

Ratish1 avatar Dec 11 '25 15:12 Ratish1