Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onError callback in useChat hook is not working. #3875

Open
Praneeth-Pike opened this issue Nov 25, 2024 · 1 comment
Open

onError callback in useChat hook is not working. #3875

Praneeth-Pike opened this issue Nov 25, 2024 · 1 comment
Labels
ai/ui bug Something isn't working

Comments

@Praneeth-Pike
Copy link

Description

Error message is not being passed to onError callback in useChat hook. I'm using a custom express server for the /chat endpoint.

Code example

// useChat hook

const { messages, input, setInput, handleSubmit, isLoading, stop, error } =
		useChat({
			id,
			api:  `${EXPRESS_API_URL}/chat`,
			initialMessages: data.messages,
			onError: (error) => {
				console.error('error on chat', error)
			},
			onFinish: async (message, { finishReason, usage }) => {
				console.log(`onFinish message id: ${message.id}`, finishReason)
				console.log('usage', usage)
			},
		})

/chat endpoint

expressApp.post('/chat', async (req: Request, res: Response) => {
	try {
		const { messages, context } = req.body

		const model = openai("gpt-4o")

		const payload = {
			model,
			messages: convertToCoreMessages([
				{
					role: 'system',
					content: context
						? `<MetaPrompt>${ChatSystemPrompt}</MetaPrompt>\n<PreviousContext>${context}</PreviousContext>`
						: `<MetaPrompt>${ChatSystemPrompt}</MetaPrompt>`,
				},
				...messages,
			]),
		}

		const result = streamText({
			...payload,
		})

		return result.pipeDataStreamToResponse(res, {
			sendUsage: true,
			getErrorMessage: (error) => {
				if (error == null) {
					return 'unknown error'
				}

				if (typeof error === 'string') {
					return error
				}

				if (error instanceof Error) {
					return error.message
				}

				return JSON.stringify(error)
			},
		})
	} catch (error: any) {
		log.error('Error in /chat:', error)
		res.status(500).json({
			error: error.message ?? 'Internal server error',
		})
	}
})

AI provider

No response

Additional context

onError works with the same code for v3.4, but when I upgrade to v4 and remove the await from streamText, onError is not being passed to the client hook

@Praneeth-Pike Praneeth-Pike added the bug Something isn't working label Nov 25, 2024
@oalexdoda
Copy link

Same issue over here. Sometimes useChat errors out (server-side) and it doesn't return any error. Maybe it's missing something here?

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ai/ui bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants