Repository navigation
StreamEnd, Message.provider_state, Optional temperature (#182) - #183
Open
johnnichev wants to merge 2 commits into
Open
johnnichev wants to merge 2 commits into
johnnichev wants to merge 2 commits into
Conversation
Providers can yield a StreamEnd as the last item of stream()/astream() to report usage (counted into agent totals, passed to on_llm_end/on_usage and the LLM_CALL trace step) and to attach an opaque provider_state to the assistant message the agent assembles from chunks. Message.provider_state round-trips through to_dict/from_dict, memory copies and the cache key. The OpenAI-compatible provider now emits it from the usage chunk it already requested on astream() and now also requests on stream(). Refs #182
…tate temperature is Optional[float] on AgentConfig, the Provider protocol and every built-in provider; None omits the field (OpenAI-compatible and Anthropic request builders), 0.0 is still sent. Default unchanged. Adds the provider_state round-trip contract test from #182, PROVIDERS.md and STREAMING.md sections, changelog entries and offline example 115. Refs #182
johnnichev
marked this pull request as ready for review
October 16, 2026 18:14
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #182 (items 1, 2, 3 and the loop test; item 5, a native Responses provider, is left for its own release).
What changed
StreamEnd(selectools.types,@beta): a provider may yield one as the last item ofstream()/astream(). The agent consumes it in all three stream consumers (_streaming_call,_astreaming_call, the native path inAgent.astream):usagegoes intoagent.usage, toon_llm_end/on_usage, and onto theLLM_CALLtrace step. Streamed turns used to hiton_llm_endwithNoneand never counted.provider_stateis copied onto the assistantMessagethe agent assembles from chunks.Consumers that pattern-match on
str/ToolCallignore it. Fallback and Router re-yield it untouched. The OpenAI-compatible provider now emits it from the usage chunk it already requested onastream(), and requests it onstream()too. A usage object only counts if it has an integerprompt_tokens, sousage: nullfrom compatible servers never produces a bogus item.Message.provider_state: Optional[Dict[str, Any]]: opaque, JSON-safe. Carried through the tool loop (the tool-call turn is appended as-is; the final no-tool turn now copies it when the message is rebuilt),ConversationMemory._copy_message(deep copy),to_dict()/from_dict(), and the cache key (only keyed when set, so existing cache entries stay valid).temperature: Optional[float]onAgentConfig, theProviderprotocol, and every built-in provider.Nonemeans omit; the OpenAI-compatible and Anthropic request builders skip the field, Gemini already toleratedNone. Default stays0.0, so nobody's output changes.0.0is still sent (zero/falsy trap covered by a test).Tests
tests/core/test_provider_state.py: field default, dict round trip, memory deep copy, cache key.tests/agent/test_stream_end.py: usage + observers + trace onrun,arun,astream; provider_state attachment; legacy providers unchanged.tests/providers/test_openai_stream_end.py: mocked Chat Completions stream, sync and async, cached tokens, no-usage case, ordering after tool calls.tests/providers/test_optional_temperature.py: agent boundary, cache key, OpenAI (4 methods), Anthropic, Gemini.tests/agent/test_provider_state_roundtrip.py: the loop test from the issue. A fake Responses-style provider returns reasoning items inprovider_statewith a function call; after the tool runs, the second provider call sees the assistant message with its state intact, onrun, streamedrun,arun, andastream.test_astreaming_call_sync_fallbackunpacked the helper's old two-tuple.Full non-e2e suite: 7551 passed, 66 skipped. mypy, ruff, bandit clean.
Notes for review
run()/arun()now record anLLM_CALLtrace step; previously onlyastream()did. Listed under Changed in the changelog.PROVIDERS.md(protocol + new section),STREAMING.md, changelog, andexamples/115_stream_end_provider.py(offline).