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

body of ModelClient.asBrowserStream wrongly typed as ReadableStream #32954

Open
2 of 6 tasks
coding101-hk opened this issue Feb 11, 2025 · 3 comments
Open
2 of 6 tasks
Assignees
Labels
Azure.Core Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@coding101-hk
Copy link

coding101-hk commented Feb 11, 2025

  • Package Name: @azure-rest/ai-inference
  • Package Version: 1.0.0-beta.5
  • Operating system:
  • nodejs
    • version: v.18.17.1
  • browser
    • name/version:
  • typescript
    • version: ^5.5.3
  • Is the bug related to documentation in

Describe the bug

I wished to pipe the body to TextDecoderStream to EventSourceParserStream, but it turned out that res.body isn't ReadableStream so body.pipeThrough(new TextDecoderStream()) fails at runtime.

However in TypeScript it's wrongly declared as ReadableStream so body.pipeThrough(new TextDecoderStream()) didn't raise any error.

Upon inspecting the inheritance chain, the body is in fact IncomingMessage -> Readable -> Stream -> EventEmitter -> Object.

To Reproduce
Steps to reproduce the behavior:

  1. Try to run the following code:
import ModelClient from '@azure-rest/ai-inference';
import { AzureKeyCredential } from '@azure/core-auth';

// override the variables here:

const BASE_URL = '...';
const API_KEY = '...';

const modelClient = ModelClient(
	`https://${BASE_URL}/models`,
	new AzureKeyCredential(API_KEY)
);

(async () => {
	const response = await modelClient
		.path('/chat/completions')
		.post({
			body: {
				model: 'DeepSeek-R1',
				max_tokens: 10000,
				messages: [{ role: 'user', content: "9/3(1+2) = ?. Don't overthink" }],
				stream: true,
			},
		})
		.asBrowserStream();
	if (!response.body) {
		throw 'connectionError';
	}
	const textStream = response.body.pipeThrough(new TextDecoderStream())
})();

Expected behavior

  1. TypeScript shows no error.
  2. No error at runtime.

Screenshots

Image

Image

Image

Additional context

The inheritance is inspected with:

export function logInheritanceInfo(obj: any) {
	let proto = obj;
	const chain = [];

	while (proto) {
		if (proto.constructor && proto.constructor.name) {
			chain.push(proto.constructor.name);
		} else {
			chain.push('[Unknown]');
		}
		proto = Object.getPrototypeOf(proto);
	}

	console.log('Inheritance Chain:', chain.join(' -> '));
}
@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Feb 11, 2025
@xirzec
Copy link
Member

xirzec commented Feb 11, 2025

@coding101-hk If I understand correctly, you are using node, but wanted to use the browser stream primitives? That's an interesting scenario if so, as I don't believe we intended asBrowserStream to be anything other than a type helper for what behavior was happening in the browser at runtime.

/cc @joheredi @deyaaeldeen @timovv

@xirzec xirzec added AI Model Inference Issues related to the client library for Azure AI Model Inference (\sdk\ai\ai-inference-rest) Client This issue points to a problem in the data-plane of the library. labels Feb 11, 2025
@github-actions github-actions bot removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Feb 11, 2025
@dargilco
Copy link
Member

FYI @glharper

@glharper glharper assigned xirzec and unassigned glharper Feb 18, 2025
@xirzec xirzec assigned timovv and unassigned xirzec Feb 18, 2025
@xirzec xirzec added Azure.Core and removed AI Model Inference Issues related to the client library for Azure AI Model Inference (\sdk\ai\ai-inference-rest) labels Feb 18, 2025
@github-actions github-actions bot added the needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team label Feb 18, 2025
@timovv
Copy link
Member

timovv commented Feb 19, 2025

As @xirzec says, asBrowserStream was intended for use in the browser environment only. In Node, asNodeStream can be used to obtain a NodeJS.ReadableStream instead. If you want a web stream version of the body in Node, you can call Readable.toWeb on the stream returned from asNodeStream and that should get you what you need.

That said, it's definitely confusing and seems incorrect that asBrowserStream returns a Node stream (which, as you note, is the wrong type) in a non-browser environment. I think we can update these as<X>Stream methods to throw when called in the wrong environment instead. Some docs to help clarify wouldn't go amiss either. I'll take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Core Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

5 participants