Skip to content

feat(extensions): let llm() call a model through a transport - #1004

Merged
ParidelPooya merged 8 commits into
mainfrom
feat/llm-transport
Dec 2, 2026
Merged

ParidelPooya merged 8 commits into
mainfrom
feat/llm-transport

Conversation

@ParidelPooya

@ParidelPooya ParidelPooya commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Why

llm() only calls Amazon Bedrock. Other hosts can run a large language model too: SageMaker endpoints, vLLM or TGI on EKS or ECS, and services outside AWS. They do not share one request format, so each needs its own call. A transport argument, like the one decider() has, lets a caller reach any of them now, and lets later built-in transports land without changing llm().

The JavaScript SDK gets the same change in aws/aws-durable-execution-sdk-js#1311.

What changes

  • llm(transport=None, *, max_tokens=1024, retry_strategy=None) sends every ask request through a transport: Callable[[LlmRequest], LlmReply].
  • bedrock_transport(config=None) is the Bedrock transport, and the default. BedrockConfig keeps model_id, client, temperature, system, and guardrail.
  • LlmRequest holds the built system prompt, the prompt (question, and the input inside <input> tags with every </input escaped), and max_tokens. So a transport does not repeat the prompt-injection handling.
  • ask treats only stop_reason="end" as complete. "max_tokens" raises LlmMaxTokensError. Any other reason, or None, raises LlmStoppedError, and the text is not checkpointed. bedrock_transport maps Converse end_turn and stop_sequence to "end" and passes other reasons through.
  • max_tokens= on llm() sets the reply limit. ask(..., max_tokens=) changes it for one call.
  • Removed, since the API has not launched: the BedrockConfig destination argument of llm(), BedrockCallOptions, and BedrockConfig.max_tokens.
  • New exports: bedrock_transport, LlmTransport, LlmRequest, LlmReply.
  • docs/ai.md and the README describe the option, with an example.

No built-in OpenAI or SageMaker transport in this PR.

Testing

  • hatch run test:all packages/aws-durable-execution-sdk-python-extensions/tests/ai: 114 passed.
  • hatch run types:check, ruff check, and ruff format pass.
  • Tests cover: the built prompt reaches the transport, max_tokens from llm() and from the call, stop reasons map to the errors, a transport error fails the step, replay does not call the transport again, bedrock_transport maps stop reasons and matches the boto3 request shape, and llm() without a transport calls Bedrock.
  • Not run against a live model.
  • No tests/e2e/ suite: the extensions package has none. The tests/ai/ tests run each handler through DurableFunctionTestRunner, including replay, which is the same harness that the other packages' e2e tests use.

Edited test expectations

Each one changed because its option was removed.

Test on main Now Why
test_ask_uses_the_config_and_the_call_overrides test_ask_uses_the_bedrock_config_and_the_call_max_tokens The per-call BedrockCallOptions override is removed. The second call now uses the configured model, with only max_tokens set per call.
test_destination_must_be_a_bedrock_config Removed The BedrockConfig destination argument is removed.
test_matches_the_boto3_request_shape Calls bedrock_transport(config) and expects LlmReply(text="ok", stop_reason="end") Llm._converse is removed. The transport returns an LlmReply.

Design note

  1. Problem. llm() can only call a model on Amazon Bedrock. A durable function whose model runs on a SageMaker endpoint, on vLLM or TGI in EKS or ECS, or outside AWS cannot use ask.
  2. Cause. ask builds the prompt, calls Converse, and checks the stop reason in one Bedrock-only method (Llm._converse). There is no point where a caller can swap the call.
  3. Options considered.
    1. Add transport next to the BedrockConfig destination: two code paths. Rejected: it keeps a Bedrock-only branch and a per-call override that a generic transport cannot use.
    2. More destination kinds (OpenAI, SageMaker): each host needs its own call and format, so llm() gains a branch per host.
    3. Built-in transports now: no host other than Bedrock has been tested live.
    4. Make Bedrock one transport and give llm() only transport: chosen.
  4. Chosen option and reason. Option 4 has one code path. ask builds the prompt, calls the transport, and checks the stop reason. Bedrock is just the default transport. A later built-in transport is one more function, with no change to llm().
  5. What the change removes or merges. Llm._converse, the destination argument, and BedrockCallOptions are removed. Prompt building and the stop-reason check stay in ask, shared by every transport.

Decisions the task did not cover (for a maintainer to accept or reject):

  • This changes the experimental llm() signature. It has not launched, so callers in this repo are updated and no breaking-change note is added.
  • The per-call override is now only max_tokens. Another model means another llm() with its own transport.
  • bedrock_transport maps only end_turn and stop_sequence to "end", and passes other reasons through. Converse never sends "end" itself: it is not in the Converse StopReason enum. So no guard is added for a raw "end".
  • The transport is synchronous and gets no abort signal, like the existing DeciderTransport. No Python ai examples exist yet, so this PR adds none.

Unverified: no live model was called through a custom transport.

Maintainer agreement: @ParidelPooya agreed to this note on 2026-12-01, per CONTRIBUTING "Write a design note before the code".

llm(transport=...) sends each ask request through the caller's own
function, for a model that is not on Amazon Bedrock. The function gets an
LlmRequest with the built system prompt, the user message with the input
in <input> tags, and max_tokens, and returns an LlmReply with the text
and a stop reason. ask maps the stop reason the same way for Bedrock and
a transport: "end" returns the text, "max_tokens" raises
LlmMaxTokensError, anything else raises LlmStoppedError.

The Bedrock path now returns the same reply shape, so a later built-in
transport needs no change to llm(). destination and transport cannot be
combined. This matches the JavaScript SDK.
- Handle a missing stop reason once, in _answer, for Bedrock and a
  transport: stop_reason stays None and the message says there is none.
- Keep the raw Bedrock stop reason. Only end_turn and stop_sequence are
  complete on Bedrock, and only "end" from a transport, so a raw "end"
  from Bedrock still fails as on main.
- Raise TypeError for max_tokens without transport, as the JavaScript
  SDK does.
- Pass a BedrockConfig on the transport path, so Llm needs no unused
  default.
- Describe LlmStoppedError.stop_reason for both sources.
@ParidelPooya
ParidelPooya marked this pull request as ready for review December 1, 2026 17:45
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime December 1, 2026 17:45 — with GitHub Actions Active
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime December 1, 2026 17:46 — with GitHub Actions Active
@github-actions

This comment has been minimized.

llm() now has one code path: every request goes through a transport.
bedrock_transport(BedrockConfig(...)) is the default transport.

- llm(transport=None, *, max_tokens=1024, retry_strategy=None).
- ask() takes max_tokens= per call. BedrockCallOptions is removed.
- BedrockConfig no longer has max_tokens.
- bedrock_transport maps Converse end_turn and stop_sequence to "end".
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime December 1, 2026 18:13 — with GitHub Actions Active
@github-actions

This comment has been minimized.

A custom transport needs no Bedrock permission.
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime December 1, 2026 18:18 — with GitHub Actions Active
@github-actions

This comment has been minimized.

A transport must map its host token-limit reason, such as length,
to "max_tokens", so ask raises LlmMaxTokensError.
The custom transport example now maps length to "max_tokens", as the
rule under it says.
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime December 1, 2026 18:31 — with GitHub Actions Active
@github-actions

This comment has been minimized.

@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime December 1, 2026 18:38 — with GitHub Actions Active
@ParidelPooya
ParidelPooya deployed to ai-pr-review-runtime December 1, 2026 18:41 — with GitHub Actions Active
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Codex AI review

Three approach findings remain:

  • packages/aws-durable-execution-sdk-python-extensions/src/aws_durable_execution_sdk_python_extensions/ai/llm.py:318: required design approval.
  • packages/aws-durable-execution-sdk-python-extensions/tests/ai/llm_test.py:365: required e2e test placement.
  • packages/aws-durable-execution-sdk-python-extensions/tests/ai/llm_test.py:143: undocumented expectation change.

No SDK correctness regression confirmed. Static review only; tests were not executed.

Reviewed commit 9fa640585d6021195c44ebe9659d0d661351ff02. Workflow run

ParidelPooya added a commit to aws/aws-durable-execution-sdk-js that referenced this pull request Dec 2, 2026
## Why

`llm()` only calls Amazon Bedrock. Other hosts can run a large language
model too: SageMaker endpoints, vLLM or TGI on EKS or ECS, and services
outside AWS. They do not share one request format, so each needs its own
call. A `transport` option, like the one `decider()` has, lets a caller
reach any of them now, and lets later built-in transports land without
changing `llm()`.

## What changes

- `llm({ transport?, maxTokens?, retryStrategy? })` sends every `ask`
request through a transport: `(request, { abortSignal }) => Promise<{
text, stopReason }>`.
- `bedrockTransport(config?)` is the Bedrock transport, and the default.
It takes the former Bedrock settings: `modelId`, `client`,
`temperature`, `system`, `guardrail`.
- The request holds the built `system` prompt, the `prompt` (question,
and the input inside `<input>` tags with every `</input` escaped), and
`maxTokens`. So a transport does not repeat the prompt-injection
handling.
- `ask` treats only `stopReason: "end"` as complete. `"max_tokens"` is
`LlmMaxTokensError`. Any other reason is `LlmStoppedError`, and the text
is not checkpointed. `bedrockTransport` maps Converse `end_turn` and
`stop_sequence` to `"end"` and passes other reasons through.
- `maxTokens` in `llm()` sets the reply limit (default 1024). `ask(...,
{ name, maxTokens })` changes it for one call.
- Removed, since the API has not launched: the `destination: { bedrock
}` option, the per-call `bedrock` override, and
`BedrockConfig.maxTokens`.
- New exports: `bedrockTransport`, `LlmTransport`, `LlmRequest`,
`LlmReply`.
- `docs/ai.md`, the README, and a new example (`llm-custom-transport`)
describe the option.

No built-in OpenAI or SageMaker transport in this PR.

## Testing

- `npm test` in the extensions package: 652 passed, 1 skipped. `tsc
--noEmit`, `biome check` and the build pass.
- Examples package: the `ai` examples pass vitest, eslint and `tsc
--noEmit`.
- Tests cover: the built prompt and abort signal reach the transport,
`maxTokens` from `llm()` and from the call, stop reasons map to the
errors, a transport error fails the attempt, replay does not call the
transport again, `bedrockTransport` maps stop reasons, `llm()` without a
transport calls Bedrock, and a custom transport works without the
Bedrock package.
- Not run against a live model.

### Edited test expectations

Each one changed because its option was removed.

| Test on main | Now | Why |
|---|---|---|
| "applies per-call overrides, a custom step name, and the system
suffix" | "applies the transport settings, a per-call maxTokens, a
custom step name, and the system suffix" | The per-call `bedrock`
override is removed. Only `maxTokens` stays per call. |
| "uses the default model when the config sets no modelId" | "uses the
default model when the transport sets no modelId" | `modelId` moved to
`bedrockTransport()`. |
| "keeps a config value when a per-call override is undefined" | "uses
the config's maxTokens when the call sets none" | Same reason. |
| "rejects a destination that is not valid", "rejects a single
unsupported destination when the factory runs" | Removed | `destination`
is removed. |

The Python SDK gets the same change in
aws/aws-durable-execution-sdk-python#1004.

## Design note

1. **Problem.** `llm()` can only call a model on Amazon Bedrock. A
durable function whose model runs on a SageMaker endpoint, on vLLM or
TGI in EKS or ECS, or outside AWS cannot use `ask`.
2. **Cause.** `ask` builds the prompt, calls Converse, and checks the
stop reason in one Bedrock-only method. There is no point where a caller
can swap the call.
3. **Options considered.**
1. *Add `transport` next to `destination`:* two code paths and a config
union. Rejected: it keeps a Bedrock-only branch and a per-call override
that a generic transport cannot use.
2. *More `destination` keys* (`openai`, `sagemaker`): each host needs
its own call and format, so `llm()` gains a branch per host.
3. *Built-in transports now:* no host other than Bedrock has been tested
live.
4. *Make Bedrock one transport and give `llm()` only `transport`:*
chosen.
4. **Chosen option and reason.** Option 4 has one code path. `llm()`
builds the prompt, calls the transport, and checks the stop reason.
Bedrock is just the default transport. A later built-in transport is one
more function, with no change to `llm()`.
5. **What the change removes or merges.** The Bedrock-only `ask` method,
the `destination` option, and the per-call `bedrock` override are
removed. Prompt building and the stop-reason check move into `llm()`,
shared by every transport.

**Decisions the task did not cover** (for a maintainer to accept or
reject):
- This changes the experimental `llm()` signature. It has not launched,
so callers in this repo are updated and no CHANGELOG entry is added.
- The per-call override is now only `maxTokens`. Another model means
another `llm()` with its own transport.
- On Bedrock, a reply with no stop reason gives `"missing"`, as on main.
- `bedrockTransport` maps only `end_turn` and `stop_sequence` to
`"end"`, and passes other reasons through. Converse never sends `"end"`
itself: it is not in the Converse `StopReason` enum. So no guard is
added for a raw `"end"`.

**Unverified:** no live model was called through a custom transport.

**Maintainer agreement:** @ParidelPooya agreed to this note on
2026-12-01, per CONTRIBUTING "Write a design note before the code".
@ParidelPooya
ParidelPooya merged commit e825bbb into main Dec 2, 2026
63 of 65 checks passed
@ParidelPooya
ParidelPooya deleted the feat/llm-transport branch December 2, 2026 00:19

This branch was successfully deployed

1 active deployment
ai-pr-review-runtime — 9fa64058 Deployed Dec 1, 2026 by ParidelPooya via ai-pr-review / Codex review / Generate Codex review #3563
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