Skip to content

Commit 01b70ea

Browse files
juliagmt-googlecopybara-github
authored andcommitted
feat: Add StreamingAfterModelCallback interface for handling streaming LLM responses
PiperOrigin-RevId: 822113783
1 parent 7f12064 commit 01b70ea

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

core/src/main/java/com/google/adk/agents/Callbacks.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ public interface AfterModelCallback extends AfterModelCallbackBase {
6666
Maybe<LlmResponse> call(CallbackContext callbackContext, LlmResponse llmResponse);
6767
}
6868

69+
/** An extended callback interface for streaming LLM responses. */
70+
public interface StreamingAfterModelCallback extends AfterModelCallback {
71+
/**
72+
* Invoked repeatedly for each significant chunk during a live stream.
73+
*
74+
* @param callbackContext Callback context.
75+
* @param llmResponse LLM response chunk.
76+
* @return modified response chunk, or empty to keep original.
77+
*/
78+
Maybe<LlmResponse> callInStream(CallbackContext callbackContext, LlmResponse llmResponse);
79+
80+
/**
81+
* Invoked once at turn completion.
82+
*
83+
* @param callbackContext Callback context.
84+
* @param finalResponse The final, aggregated LLM response.
85+
* @return modified final response, or empty to keep original.
86+
*/
87+
Maybe<LlmResponse> callAtStreamEnd(CallbackContext callbackContext, LlmResponse finalResponse);
88+
89+
/** Defaults to the end-of-stream behavior. */
90+
@Override
91+
default Maybe<LlmResponse> call(CallbackContext callbackContext, LlmResponse llmResponse) {
92+
return callAtStreamEnd(callbackContext, llmResponse);
93+
}
94+
}
95+
6996
/**
7097
* Helper interface to allow for sync afterModelCallback. The function is wrapped into an async
7198
* one before being processed further.

0 commit comments

Comments
 (0)