4141 get_system_prompt ,
4242)
4343from utils .mcp_headers import mcp_headers_dependency
44+ from utils .shields import detect_shield_violations , get_available_shields
4445from utils .token_counter import TokenCounter
4546from utils .transcripts import store_transcript
4647from utils .types import TurnSummary , ToolCallSummary
@@ -243,6 +244,13 @@ async def response_generator( # pylint: disable=too-many-branches,too-many-stat
243244 elif event_type == "response.completed" :
244245 # Capture the response object for token usage extraction
245246 latest_response_object = getattr (chunk , "response" , None )
247+
248+ # Check for shield violations in the completed response
249+ if latest_response_object :
250+ detect_shield_violations (
251+ getattr (latest_response_object , "output" , [])
252+ )
253+
246254 if not emitted_turn_complete :
247255 final_message = summary .llm_response or "" .join (text_parts )
248256 if not final_message :
@@ -348,11 +356,11 @@ async def retrieve_response(
348356 Asynchronously retrieves a streaming response and conversation
349357 ID from the Llama Stack agent for a given user query.
350358
351- This function configures input/output shields, system prompt,
352- and tool usage based on the request and environment. It
353- prepares the agent with appropriate headers and toolgroups,
354- validates attachments if present, and initiates a streaming
355- turn with the user's query and any provided documents.
359+ This function configures shields, system prompt, and tool usage
360+ based on the request and environment. It prepares the agent with
361+ appropriate headers and toolgroups, validates attachments if
362+ present, and initiates a streaming turn with the user's query
363+ and any provided documents.
356364
357365 Parameters:
358366 model_id (str): Identifier of the model to use for the query.
@@ -365,7 +373,8 @@ async def retrieve_response(
365373 tuple: A tuple containing the streaming response object
366374 and the conversation ID.
367375 """
368- logger .info ("Shields are not yet supported in Responses API." )
376+ # List available shields for Responses API
377+ available_shields = await get_available_shields (client )
369378
370379 # use system prompt from request or default one
371380 system_prompt = get_system_prompt (query_request , configuration )
@@ -402,6 +411,10 @@ async def retrieve_response(
402411 if query_request .conversation_id :
403412 create_params ["previous_response_id" ] = query_request .conversation_id
404413
414+ # Add shields to extra_body if available
415+ if available_shields :
416+ create_params ["extra_body" ] = {"guardrails" : available_shields }
417+
405418 response = await client .responses .create (** create_params )
406419 response_stream = cast (AsyncIterator [OpenAIResponseObjectStream ], response )
407420
0 commit comments