Completion result: {completion}
);
}
```
### useObject
For consuming text streams that represent JSON objects and parsing them into a complete object based on a schema.
- Works with agent text streams i.e. `.toTextStreamResponse()`
- The useObject `api` defaults to `/api/completion`
- Works with the Mastra REST API agent stream endpoint `{MASTRA_BASE_URL}/agents/:agentId/stream` for text streams,
i.e. structured output is defined.
```typescript filename="app/api/use-object/route.ts" copy
import { mastra } from '@/src/mastra';
export async function POST(req: Request) {
const { messages } = await req.json();
const myAgent = mastra.getAgent('weatherAgent');
const stream = await myAgent.stream(messages, {
output: z.object({
weather: z.string(),
}),
});
return stream.toTextStreamResponse();
}
```
```typescript
import { experimental_useObject as useObject } from '@ai-sdk/react';
export default function Page() {
const { object, submit } = useObject({
api: '/api/use-object',
schema: z.object({
weather: z.string(),
}),
});
return (