文件历史

提交图

159 次代码提交

作者 SHA1 备注 提交日期
Simon Willison beaec1e20c Lint fixes: mypy, ruff, black
- Drop the placeholder messages() declaration on _BaseResponse so
  AsyncResponse.messages() (an async coroutine) no longer trips
  the mypy override check. text/json/tool_calls already follow
  this pattern.
- Remove three unused imports flagged by ruff in test_parts.py.
- Apply pending black reformats across the tree.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 14:27:30 -07:00
Simon Willison b97a6902f5 Persist visible reasoning to logs and render in markdown
Adds a `reasoning` column to the responses table (migration m022)
populated from concatenated visible-reasoning text in the assembled
message. `llm logs --md` renders it under a `## Reasoning` heading
above the response when present.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 14:27:22 -07:00
Simon Willison 842ab2a93f response.messages is a method, matching .text() / .json() / .tool_calls()
Sync: response.messages() forces execution if not drained, so callers
no longer have to remember to call .text() first. Async: `await
response.messages()` awaits the force.

Internal sync paths (_response_to_dict, _chain_for_tool_results,
_build_full_chain, Response.reply, AsyncResponse.reply) use a new
private _messages_now() helper that assumes the response is already
drained, so they don't have to await on async responses.

Drops the now-obsolete "accessing .messages on un-awaited
AsyncResponse raises" parity test — that constraint goes away with
the method form.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 14:04:35 -07:00
Simon Willison f3a0962162 response.reply() auto-executes pending tool calls
Zero-arg sugar: when a response made tool calls and tool_results= is
not passed, reply() runs self.execute_tool_calls() and threads the
results into the next turn. Pass tool_results= explicitly to skip
the auto-execute path (e.g. for mutated or synthetic results). Also
forwards self.prompt.tools to the next turn so the model can call
the same tools again, mirroring Conversation.prompt's tools-or-self
rule.

AsyncResponse.reply() is now an awaitable coroutine — `await
response.reply(...)` — so the auto-execute path can `await
self.execute_tool_calls()` internally. This is a non-shipped API
break: existing async-reply callers in the test suite updated.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 13:51:13 -07:00
Simon Willison a2547d8183 Drop token_count from ReasoningPart, use redacted marker StreamEvent
ReasoningPart.token_count duplicated info already on response.token_details
(reasoning_tokens), and the side-channel `response._reasoning_token_count`
attribute with its set_usage ordering footgun was the wrong shape. Replaced
with a clean StreamEvent.redacted=True marker that plugins yield like any
other event. The framework hoists redacted reasoning Parts to the start of
the assembled message so UIs render them before content, even though the
opaque count typically arrives at the end of the stream.

Also fix parallel tool calls emitted without tool_call_id (e.g. Gemini):
a fresh tool_call_name now always allocates a new index instead of falling
through to the prior tool-call group, so N parallel calls produce N
distinct ToolCallParts instead of one with concatenated names and args.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-28 13:28:55 -07:00
Simon Willison 3b0d0fa0a5 part_index is now (mostly) automatically assigned 2026-04-28 09:35:53 -07:00
Simon Willison de63d8b69e Fixes for ruff 2026-04-22 10:13:39 -07:00
Simon Willison 38cf65adb1 Remove unneccassry exception catch 2026-04-22 10:10:45 -07:00
Simon Willison 5a92cdfc6e Improved some comments
Had a different model review them for accuracy
2026-04-22 08:55:38 -07:00
Simon Willison 211e678e07 Cleaned up tests and comments
Removed all mentions of 'phase'
2026-04-22 08:43:33 -07:00
Simon Willison afc41b0c47 mypy fixes 2026-04-21 21:37:04 -07:00
Simon Willison 65b8e37c79 Ran Black 2026-04-21 21:10:13 -07:00
Simon Willison 3df8e4426b Chain tool-result turns now carry system + system_fragments forward
Bug: ChainResponse.responses() and AsyncChainResponse.responses()
built the follow-up Prompt (for tool-result turns inside the chain
loop) without propagating system= or system_fragments= from the
initial prompt. Adapters that read prompt.system directly — OpenAI's
Chat, for example, which sends system as its own message — saw an
empty system on every turn after the first, silently losing the
caller's instruction.

Fix: pass system=self.prompt._system and
system_fragments=self.prompt.system_fragments when constructing the
next Prompt. Same change on sync and async paths.

_chain_for_tool_results keeps building messages= from the prior
response's prompt.messages + messages, so adapters that read
prompt.messages (the Phase 7 canonical input) continue to work too —
this fix is specifically for the adapters that still use the
prompt.system legacy field.

Three regression tests (sync, sync+system_fragments, async) pin the
new behavior.

670 tests in llm core; llm-anthropic (32) and llm-gemini (50) still
green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 15:55:36 -07:00
Simon Willison bb5daaf6d8 Add messages= parameter to chain() (sync + async, Conversation + Model)
Parity with prompt(): all four chain() methods —
Conversation.chain, AsyncConversation.chain, _Model.chain,
_AsyncModel.chain — now accept a messages= kwarg and pre-bake the
full chain via _build_full_chain so the first response of the chain
loop satisfies the invariant response.prompt.messages == what was
sent.

Semantics match prompt() exactly: when messages= is passed, it's
authoritative for the first turn. The prompt= kwarg is ignored for
chain construction (it stays available via prompt.prompt / .system /
.attachments for any legacy plugin code). Subsequent tool-result
turns inside the chain loop still extend the chain via
_chain_for_tool_results, which reads from the prior response's
prompt.messages + messages.

Six new tests cover: conv.chain(messages=), model.chain(messages=),
messages= authoritative over prompt= kwarg, explicit messages=
replaces conversation history, and the async variants of all of the
above.

667 tests in llm core (661 before + 6 new); llm-anthropic (32) and
llm-gemini (50) still green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 15:15:39 -07:00
Simon Willison 253fed259b Add llm/serialization.py — TypedDicts for the wire form
Adds a dedicated module describing the exact JSON-safe shape returned
by Part.to_dict() / Message.to_dict() / Response.to_dict() and accepted
by the matching from_dict methods. Every consumer that reads or writes
serialized llm data can now import a specific TypedDict and get proper
autocomplete, static type-checking, and schema generation support.

Module: llm/serialization.py (deliberately not "schema" — that name is
taken by the structured-output feature).

  TextPartDict, ReasoningPartDict, ToolCallPartDict,
  ToolResultPartDict, AttachmentPartDict
    — one per Part subclass, each discriminated by a
      Literal["<type>"] on the `type` field so pydantic/type-checkers
      can narrow cleanly.

  PartDict = Union[...]
    — the discriminated-union form of all Part dicts.

  AttachmentDict — the nested attachment payload (base64 content when
    bytes were supplied).

  MessageDict — {role, parts: list[PartDict], provider_metadata?}

  PromptDict, UsageDict, ResponseDict — full Response.to_dict() shape
    including the input chain, options, messages, and audit fields.

TypedDicts use typing_extensions.NotRequired (available for 3.10+ via
a transitive pydantic dep) so Python 3.10 consumers work.

Type annotations on every .to_dict() / .from_dict() method across
parts.py and models.py now reference the specific TypedDict rather
than Dict[str, Any]. Consumers writing

    def save_messages(msgs: list[MessageDict]) -> None: ...

get autocomplete on msgs[i]["role"], type-errors on typos, and pydantic
TypeAdapter-based validation works out of the box:

    from pydantic import TypeAdapter
    from llm.serialization import MessageDict
    TypeAdapter(MessageDict).validate_python(incoming)       # validate
    TypeAdapter(MessageDict).json_schema()                   # export

Also tidied _response_to_dict to omit usage.details when None so the
serialized UsageDict doesn't carry a null field where pydantic would
reject it during validation.

New test_serialization.py (41 tests):
  - required/optional key sets on every TypedDict
  - actual .to_dict() output conforms to its TypedDict via TypeAdapter
  - PartDict discriminated union accepts all 5 Part variants and
    rejects unknown types
  - Literal discriminator values are correct
  - method annotations point at the right TypedDicts
  - JSON round-trip of Response.to_dict() validates

661 total tests pass (620 before + 41 new). llm-anthropic (32) and
llm-gemini (50) still green against the editable llm.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 10:20:29 -07:00
Simon Willison c975d4ce7f Fix llm -c regression: rehydrated response.messages preserves text+tools
_build_parts now falls back to synthesizing from self._chunks and
self._tool_calls when self._stream_events is empty. That's the shape
of a Response rehydrated via from_row (SQLite doesn't persist
StreamEvents under Phase 1-7 scope).

Without this, Conversation.prompt's full-chain construction on a
follow-up turn (llm -c, load_conversation().prompt(...)) produced
[user(q1), user(q2)] — dropping the assistant turn entirely — because
prev.messages was []. Now prev.messages yields
[assistant(text + tool calls)] and the chain is correct:
[user(q1), assistant(a1), user(q2)].

Reasoning signatures and structured reasoning parts are still lost
on SQLite rehydrate — that requires Phase 8 (structured parts
persistence) or use of response.to_dict() / from_dict() for
structure-preserving serialization. For the common case (text-only
multi-turn), llm -c works again.

Regression tests pin the fallback + the end-to-end load_conversation
follow-up.

602 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 08:37:49 -07:00
Simon Willison 206197d395 Phase 7 follow-up: ChainResponse pre-bakes tool-result chain
_chain_for_tool_results() builds the full chain for a tool-result
turn inside a chain loop: prior response's full input + output +
a tool-role message carrying the new results + any attachments.

ChainResponse.responses() and AsyncChainResponse.responses() now
pass that chain as messages= when constructing the next Response.

Why: under the Phase 7 invariant, response.prompt.messages is what
the model sees. Without this, the tool-result turn's prompt.messages
would only synthesize a single tool-role message from the legacy
tool_results= kwarg — stripping reasoning signatures and tool-call
thoughtSignatures from the prior assistant turn. That breaks
multi-turn Gemini 3 tool loops (thoughtSignature must be echoed)
and Claude extended thinking inside chains.

All 600 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 08:22:09 -07:00
Simon Willison 63cdf94fbe Phase 7: to_dict/from_dict/reply + full-chain invariant
Three new Response primitives that make conversation persistence
outside SQLite ergonomic — and a clarified invariant that
response.prompt.messages is always exactly what the model was sent.

response.reply(text, **kwargs) -> Response
  Builds next-turn chain = self.prompt.messages + self.messages +
  [user(text)] and calls self.model.prompt(messages=chain, ...).
  Works from any Response, regardless of origin (conversation,
  standalone model.prompt, or from_dict-rehydrated).

response.to_dict() / Response.from_dict(data, *, model=None)
  JSON-safe serialization. to_dict captures model_id, input chain
  (prompt.messages — full), assembled output (response.messages
  including reasoning parts and provider_metadata signatures),
  options, and optional audit fields (id, usage, datetime_utc).
  from_dict rehydrates a _done=True Response where text() returns
  the answer and messages returns the full structured view without
  re-running the assembler. Async variants likewise.

Full-chain invariant: response.prompt.messages == what was sent.
  - Conversation.prompt / AsyncConversation.prompt now pre-compute
    the full chain (last response's prompt.messages + last
    response's messages + new user turn, or tool_results for
    chain loops) and pass it as messages= into the Prompt.
  - Prompt.messages, when _explicit_messages is set, returns that
    list verbatim. The previous "combine with prompt= trailing
    user" behavior is dropped (was Phase 3 scaffolding, no user
    ever wants a partial chain).
  - _BaseConversation._build_full_chain is the shared builder.

OpenAI adapter simplified: build_messages now reads only
prompt.messages and ignores conversation.responses. Under the
invariant, prompt.messages already has the full history baked in;
walking conversation would double-emit.

response.messages now short-circuits to _loaded_messages when set
(rehydrated responses don't re-run the assembler).

The persistence pattern becomes:

    response = model.prompt("Hi", thinking=True)
    response.text()
    Path("chat.json").write_text(json.dumps(response.to_dict()))

    # Later, any process:
    data = json.loads(Path("chat.json").read_text())
    response = llm.Response.from_dict(data)
    response = response.reply("Follow up")
    print(response.text())

Reasoning signatures (Anthropic extended thinking, Gemini
thoughtSignature) round-trip via provider_metadata on the
ReasoningPart / ToolCallPart — so multi-turn extended thinking
works across process boundaries for free.

19 new tests + 2 tightened existing tests; 600 total pass.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-21 08:17:31 -07:00
Simon Willison 476d5ef989 Phase 3: messages= parameter and Prompt.messages synthesis
Add messages= kwarg to model.prompt(), conversation.prompt(), and
their async counterparts. The list flows through to Prompt.__init__
as _explicit_messages.

Prompt.messages is now a property that returns one uniform
list[Message] regardless of which surface the caller used:

  - If messages= was passed explicitly, that list is returned
    verbatim (with any prompt= string appended as a trailing user
    TextPart, matching how system= sugars into a leading system
    Message).
  - Otherwise synthesized from system=, prompt=, attachments=, and
    tool_results=. Plugins read one representation; callers keep all
    the existing ergonomic entry points.

No plugin changes yet — existing adapters keep reading the legacy
prompt.prompt / prompt.system / prompt.attachments. Phase 4 is when
the built-in OpenAI plugin switches to reading prompt.messages.

543 tests passing; no regressions.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 09:33:52 -07:00
Simon Willison fc7fb28f88 Phase 2: Response streaming scaffolding (stream_events, messages)
Teach Response and AsyncResponse to accept str | StreamEvent from
execute(). Plain-str plugins keep working unchanged — their yields are
wrapped as StreamEvent(type="text", chunk=..., part_index=0) internally.

New capabilities:
  - response.stream_events() / response.astream_events() yield every
    event (text, reasoning, tool_call_*, tool_result) as the model
    produces it. Iteration ("for chunk in response") still yields
    only text strings.
  - response.messages returns the list of assembled Message objects
    once the response is done. AsyncResponse.messages raises if not
    yet awaited.
  - _BaseResponse._build_parts() groups events by part_index into
    typed Parts (TextPart, ReasoningPart, ToolCallPart, ToolResultPart).
    Mixing families at the same index raises ValueError.
  - Opaque reasoning token counts: plugins set
    response._reasoning_token_count = N; _build_parts prepends a
    ReasoningPart(redacted=True, token_count=N, text="").
  - provider_metadata merges across events for the same part
    (last non-None wins per top-level namespace key).

ChainResponse.stream_events and AsyncChainResponse.astream_events
pass through from each underlying response.

530 tests passing; no regressions to the existing suite.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 09:31:30 -07:00
Simon Willison 946e433698 More docstrings and autoclass embeds in docs 2026-03-31 13:12:32 -07:00
Simon Willison 1562a8f444 Added some more autoclass docs, with new doctrings 2026-03-31 12:54:00 -07:00
Eric Bloch f7934c5c26 Fix some descriptor leaks (#1313)
Refs #1312
2025-12-11 14:20:58 -08:00
Giuseppe Rota 618463de13 fix: register fragment source in chat command (#1316)
* Register fragment source in chat command
2025-12-11 13:54:32 -08:00
Simon Willison c7abd58000 Fix for asyncio.iscoroutinefunction warning 2025-11-14 15:49:18 -08:00
Simon Willison 08094082f2 Toolbox.add_tool(), prepare() and prepare_async() methods
Closes #1111
2025-08-11 13:19:31 -07:00
Simon Willison 3a96d52895 Better handling of before_call cancellation, closes #1148 2025-06-01 18:36:55 -07:00
Simon Willison d96ae4ed8d Fix --async logging to database, closes #1150 2025-06-01 17:38:26 -07:00
Simon Willison 30e0c4abe8 ToolResult.exception for tool errors, now logged to DB
Closes #1104
2025-06-01 17:01:40 -07:00
Joe Freeman 94c62f45b1 Fix type of tools argument (#1144)
* Fix type of 'tools'
2025-06-01 12:06:20 -07:00
Simon Willison ed64fc3362 chain_limit/before_call/after_call for conversations
* chain_limit/before_call/after_call for conversations, closes #1088
* Docs for before_call/after_call including for model.conversation
2025-06-01 12:00:29 -07:00
Simon Willison b5d1c5ee90 Tools can now return attachments
Closes #1014

- llm.ToolOutput(output='...', attachments=[...]) for tools to return attachments
- New table: `tool_results_attachments`
- Table is populated when tools return attachments
- llm --tools-debug shows attachments returned by tools
- llm logs shows attachments returned by tools
2025-06-01 10:08:36 -07:00
Simon Willison f74e242442 Clarifying comment 2025-06-01 09:16:37 -07:00
Simon Willison b858b0083e set_resolved_model() for async models, closes #1117 2025-05-28 07:39:57 -07:00
Simon Willison 301db6d76c responses.resolved_model column and response.set_resolved_model(model_id) method, closes #1117 2025-05-28 07:17:03 -07:00
Simon Willison 6bab712cdd Fix for module 'builtins' has no attribute 'instance_id, closes #1107 2025-05-27 13:13:28 -07:00
Simon Willison 9e25055765 Turn incorrect tool names into errors, closes #1104 2025-05-27 09:29:05 -07:00
Simon Willison e4ecb86421 Log tool_instances to database (#1098)
* Log tool_instances to database, closes #1089
* Tested for both sync and async models
2025-05-26 21:01:55 -07:00
Simon Willison 1dc7a1d1f9 Monotonic ULIDs, refs #1099 2025-05-26 19:49:42 -07:00
Simon Willison a87e4505ff Remove obsolete details: mechanism from ChainResults, closes #1087 2025-05-25 22:43:45 -07:00
Simon Willison bb336d33a0 Toolbox class for class-based tool collections (#1086)
* Toolbox class for class-based tool collections

Refs #1059, #1058, #1057
2025-05-25 22:42:52 -07:00
Arjan Mossel 5d6f96a908 Provide response_json type hint (#1077) 2025-05-24 13:46:36 -07:00
Simon Willison 36477cf9e5 llm chat -c and llm -c carry forward tools, closes #1020 2025-05-23 21:10:51 -07:00
Simon Willison d2886d4692 Record which plugin a tool came from, including in DB - refs #1020 2025-05-23 15:44:12 -07:00
Simon Willison 3e3492898c Conversations using tools in Python API
Refs #1033
2025-05-21 23:05:16 -07:00
Simon Willison e172e9e52d Clean up one more comment 2025-05-21 21:42:46 -07:00
Simon Willison 3cb875fa3d Async tool support (#1063)
* Sync models can now call async tools, refs #987
* Test for async tool functions in sync context, refs #987
* Test for asyncio tools, plus test that they run in parallel
* Docs for async tool usage
2025-05-21 21:42:19 -07:00
Simon Willison bd2180df7d llm chat --tool and --functions (#1062)
* Tool support for llm chat, closes #1004
2025-05-20 21:30:27 -07:00
Simon Willison 2df619e7d8 --chain-limit option, closes #1025 2025-05-13 21:52:39 -04:00
Simon Willison c81f0560e0 Fixed remaining mypy problems, refs #1023
Refs https://github.com/simonw/llm/pull/996#issuecomment-2878191352
2025-05-13 17:19:30 -07:00