Overview
The/api/genai-agent endpoint is the primary communication interface for the ADK agent. It processes chat messages and returns streaming responses using the GenAIAgentService from @yagolopez/adk-utils.
This endpoint handles:
- Message validation
- Agent initialization and execution
- Streaming response generation
- Error handling with appropriate HTTP status codes
Endpoint
application/json
Request
Array of chat messages in AI SDK format. Each message should contain:
role: “user” | “assistant” | “system”content: string - the message text- Optional additional fields like
id,createdAt, etc.
Request Schema
Request Examples
Response
Success Response (200)
Returns a streaming response with AI SDK format. The response body streams the agent’s reply in real-time.Streaming response containing the agent’s generated text in AI SDK streaming format
Error Responses
Returned when messages are missing or invalid
Returned when an unexpected error occurs during processing
Implementation
The route handler uses theGenAIAgentService from @yagolopez/adk-utils to manage agent communication:
Key Components
GenAIAgentService
The service class from
@yagolopez/adk-utils that provides:validateMessages()- Validates the message arraycreateStreamingResponse()- Generates streaming AI responsescreateErrorResponse()- Creates standardized error responses
rootAgent
The main ADK agent instance imported from
/app/agents/agent1 that processes user messages and generates responsesError Handling
The endpoint implements comprehensive error handling:- Message Validation: Checks if messages array is valid before processing
- Try-Catch Block: Catches any runtime errors during execution
- Standardized Responses: Uses
GenAIAgentService.createErrorResponse()for consistent error formatting - Logging: Errors are logged to console for debugging
Usage in Client
Here’s a complete example of integrating this endpoint in a React component:Related
- GenAIAgentService - Service class documentation
- Agent Configuration - How to configure agents
- Streaming Responses - Understanding streaming responses