Repository navigation
feat(py): add .reasoning, ModelResponse.total_usage, and multipart Part.from_tool_response - #48
Conversation
1379dbb to
a0bc468
Compare
a0bc468 to
1aee91c
Compare
fa6ce23 to
8f25d33
Compare
1aee91c to
18f884b
Compare
8f25d33 to
0588ca1
Compare
18f884b to
b39e746
Compare
0588ca1 to
09d6041
Compare
b39e746 to
1548b16
Compare
09d6041 to
d5c173d
Compare
6799219 to
b1d8e06
Compare
42f95c4 to
461fd3f
Compare
b1d8e06 to
3ae5af8
Compare
461fd3f to
9f173e3
Compare
3ae5af8 to
6491461
Compare
9f173e3 to
5155fe0
Compare
6491461 to
7834a9d
Compare
5155fe0 to
8bf395f
Compare
b0db9b6 to
c3aa280
Compare
f720783 to
c0bfbec
Compare
c3aa280 to
c65bb39
Compare
c0bfbec to
3cb93cf
Compare
95ec0ef to
a59c6ef
Compare
3cb93cf to
1bced54
Compare
a59c6ef to
9d037ae
Compare
…quest, and multipart tool responses
1bced54 to
b85f80c
Compare
hilariie
left a comment
There was a problem hiding this comment.
This needs a rebase - main added its own generate_loop_test.py in #83, so the new test file is an add/add conflict. There's also no test where a middleware calls next() twice; swapping sent[-1] for sent[0], or counting usage once per turn, still passes every test here. Also, the reasoning and from_tool_response tests would sit better next to the existing accessor tests in model_test.py and part_test.py. sse_failed_response_test.py doesn't exercise SSE, and the existing BoxedFailure tests are in to_sse_test.py.
Resolves the add/add conflict on generate_loop_test.py and addresses review: - total_usage comes from a usage sink every model action reports to, so Fallback's direct backup call and nested generates in tools count. - Part.from_tool_response dumps output/metadata and validates content the same way the tool runner does (helpers moved down into _core/_model.py). - Drop the BoxedFailure retry_after_ms/reason change: nothing reads it. - Add a middleware-calls-next()-twice test; move reasoning and from_tool_response tests into model_test.py and part_test.py. - Drop "as JS does" / "Same as JS" comparisons from comments.
|
Merged main in 4ba3255 (no force push). The two generate_loop_test.py files are combined. Added |
Docs used to be inlined into the last user message in turn_request, before any wrap_model ran, so clearing request.docs in middleware didn't keep them from the model and response.request under-reported what was sent. Inlining now runs in Action._run for model actions, as in Go, so every route to a model gets it: the turn loop, Fallback's direct backup call, a judge model. augment_with_context moved to _core/_model.py.
…se and as a keyword
Adds
.reasoningonModelResponse,Message, andModelResponseChunk, populatesModelResponse.total_usagewith every model call made under agenerate, echoes the post-middleware request onresponse.request, and letsPart.from_tool_responsebuild multipart tool responses the same way the tool runner does.Main Changes
.reasoningon responses, messages, and chunks:.textalready skips reasoning parts, so reading the model's thinking meant iteratingmessage.content..reasoningconcatenates reasoning parts and returns''when there are none. Go hasresp.Reasoning(); JS hasresponse.reasoning.total_usagecounts every model call under the run: Previouslytotal_usagewas alwaysNone, so a tool loop reported only its last call. Every model action now reports its usage to a context-scoped sink thatgenerateinstalls, so calls made outsidenext()count too: Fallback calls the backup model directly, and a tool can run its owngenerate.usagestays the call that produced the reply, sototal_usage >= usageholds. This follows Go'sTotalUsage; JS never setstotalUsage.response.requestechoes the request afterwrap_model; docs are inlined model-side: Previouslyresponse.requestwas the request before middleware, anddocs=were inlined into the last user message before anywrap_modelran, so clearingrequest.docsin middleware didn't keep them from the model. Inlining now runs in the model action itself, after everywrap_model, as in Go. Every route to a model gets it, including Fallback's direct backup call and a judge model called from middleware, and a request that already carries the context part isn't inlined twice.response.requestcarries the request the model action last received (the lastnext()call when a middleware retries);messagesstays the resendable history. JS also inlines model-side (augmentWithContext), but behind asupports.contextcheck; Python always inlines, on purpose.Part.from_tool_responsematches the tool runner: Previously it accepted onlyoutputand kept it as-is, so a manual loop (return_tool_requests=True) had to hand-dumpcontent, and a Pydantic output reached plugins as a model object (Bedrock fails to serialize it; plugins callingstr()send the repr). It now acceptscontent: Sequence[Part], unwrapsMultipartToolResponse, dumpsoutputandmetadatato JSON, and raisesINVALID_ARGUMENTon empty or non-serializable parts, using the same helpers as the tool runner. Settingcontentormetadataboth on theMultipartToolResponseand as a keyword also raisesINVALID_ARGUMENTinstead of dropping one.Breaking changes
wrap_modelno longer sees doc text inmessages: docs reachwrap_modelonly onrequest.docs; the context part is added after the last middleware. Middleware that scanned message text for doc content must readrequest.docsinstead. Clearingrequest.docsinwrap_modelnow keeps the docs from the model.