diff --git a/src/albedo/harness/extensions/mistral/albedo_mistral.erl b/src/albedo/harness/extensions/mistral/albedo_mistral.erl index 4012b5bc..71a78901 100644 --- a/src/albedo/harness/extensions/mistral/albedo_mistral.erl +++ b/src/albedo/harness/extensions/mistral/albedo_mistral.erl @@ -334,29 +334,34 @@ chat_models(Body) -> %% One streamed chunk in the shape the shared reducer reads: a thinking %% model's `delta.content` is a list of `thinking` and `text` chunks, which -%% become `reasoning_content` and `content`. Anything else passes unchanged; -%% a chunk whose JSON has no content list is not decoded at all. +%% become `reasoning_content` and `content`, and the delta keeps only the +%% fields the reducer reads (Mistral's carries `index`, `tool_call_id` and +%% `metadata` too). A chunk that needs neither passes unchanged. chunk(Data) -> - case binary:match(Data, [<<"\"thinking\"">>, <<"\"text\"">>, <<"\"content\":[">>, - <<"\"content\": [">>]) of - nomatch -> Data; - _ -> - case decode(Data) of - #{<<"choices">> := Choices} = Chunk when is_list(Choices) -> - iolist_to_binary(json:encode(Chunk#{<<"choices">> => [choice(C) || C <- Choices]})); - _ -> Data - end + case decode(Data) of + #{<<"choices">> := Choices} = Chunk when is_list(Choices) -> + case [choice(C) || C <- Choices] of + Choices -> Data; + Read -> iolist_to_binary(json:encode(Chunk#{<<"choices">> => Read})) + end; + _ -> Data end. -choice(#{<<"delta">> := #{<<"content">> := Parts} = Delta} = Choice) when is_list(Parts) -> +-define(DELTA_FIELDS, [<<"role">>, <<"content">>, <<"tool_calls">>, <<"reasoning_content">>]). + +choice(#{<<"delta">> := Delta} = Choice) when is_map(Delta) -> + Choice#{<<"delta">> => content(maps:with(?DELTA_FIELDS, Delta))}; +choice(Choice) -> + Choice. + +content(#{<<"content">> := Parts} = Delta) when is_list(Parts) -> Thinking = iolist_to_binary([thinking_text(P) || #{<<"type">> := <<"thinking">>} = P <- Parts]), Text = iolist_to_binary([T || #{<<"type">> := <<"text">>, <<"text">> := T} <- Parts, is_binary(T)]), Content = maps:remove(<<"content">>, Delta), WithText = case Text of <<>> -> Content; _ -> Content#{<<"content">> => Text} end, - WithThinking = case Thinking of <<>> -> WithText; _ -> WithText#{<<"reasoning_content">> => Thinking} end, - Choice#{<<"delta">> => WithThinking}; -choice(Choice) -> - Choice. + case Thinking of <<>> -> WithText; _ -> WithText#{<<"reasoning_content">> => Thinking} end; +content(Delta) -> + Delta. thinking_text(#{<<"thinking">> := Inner}) when is_list(Inner) -> [case I of diff --git a/test/e2e/mistral_test.py b/test/e2e/mistral_test.py index 7d2ac766..0883a54c 100644 --- a/test/e2e/mistral_test.py +++ b/test/e2e/mistral_test.py @@ -79,7 +79,15 @@ def thinking(text): THOUGHTFUL = Reply( "text", events=[ - chunk({"role": "assistant", "content": [thinking("six sevens")]}), + # Mistral's delta also carries fields the shared reducer does not read. + chunk( + { + "role": "assistant", + "content": [thinking("six sevens")], + "index": 0, + "tool_call_id": None, + } + ), chunk({"content": [thinking(" are 42"), {"type": "text", "text": "It is "}]}), chunk({"content": "42."}), chunk(