mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-12 23:01:41 +00:00
fix(guardrails): append assistant turn to structured_messages in post_call
This commit is contained in:
parent
72a461ba4a
commit
de9140b8ee
1 changed files with 22 additions and 0 deletions
|
|
@ -348,6 +348,28 @@ class OpenAIChatCompletionsHandler(BaseTranslation):
|
|||
if hasattr(response, "model") and response.model:
|
||||
inputs["model"] = response.model
|
||||
|
||||
# Build structured_messages: original request messages + assistant
|
||||
# response turn(s) appended, so post-call guardrails see full context.
|
||||
original_messages = cast(
|
||||
List[AllMessageValues],
|
||||
request_data.get("messages", []) if request_data else [],
|
||||
)
|
||||
structured_messages: List[AllMessageValues] = list(original_messages)
|
||||
for choice in response.choices:
|
||||
msg = choice.message
|
||||
if msg.tool_calls or msg.content is not None:
|
||||
assistant_turn: dict = {"role": "assistant"}
|
||||
if msg.content is not None:
|
||||
assistant_turn["content"] = msg.content
|
||||
if msg.tool_calls:
|
||||
assistant_turn["tool_calls"] = [
|
||||
tc.model_dump() if hasattr(tc, "model_dump") else tc
|
||||
for tc in msg.tool_calls
|
||||
]
|
||||
structured_messages.append(cast(AllMessageValues, assistant_turn))
|
||||
if structured_messages:
|
||||
inputs["structured_messages"] = structured_messages
|
||||
|
||||
guardrailed_inputs = await guardrail_to_apply.apply_guardrail(
|
||||
inputs=inputs,
|
||||
request_data=request_data,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue