Asked 1 month ago by NeutronWanderer749
Why does my Azure OpenAI GPT-4 image analysis .NET call hang while Python responds quickly?
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by NeutronWanderer749
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I have a .NET Core application that uses the Azure OpenAI GPT-4o model to analyze an image.
My C# code hangs when calling CompleteChatAsync(), with no response even after 5 minutes, while the equivalent Python code returns a response within 5-6 seconds.
Here is the .NET code:
CSHARPusing Azure; using Azure.AI.OpenAI; using OpenAI; using OpenAI.Chat; using System; using System.ClientModel; using System.Collections.Generic; using System.IO; using System.Threading.Tasks; // Azure OpenAI client library for .NET - version 2.1.0 public class AzureOpenAiService : IAzureOpenAiService { private static string endpoint = "https://xyz.openai.azure.com/"; private static string deployment = "gpt-4o"; private static string apiKey = "LFK"; public async Task FindPrimarySubjectAsync(string imagePath) { try { string base64Image = EncodeImage(imagePath); var credential = new AzureKeyCredential(apiKey); OpenAIClientOptions openAIClientOptions = new OpenAIClientOptions { Endpoint = new Uri(endpoint) }; var client = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey)); var chatMessages = new List<ChatMessage> { new SystemChatMessage("Analyze the uploaded image and return a single-word description of the main subject. The response should be only one word, representing the most general yet accurate category."), new UserChatMessage($"What is in this image? Image: data:image/png;base64,{base64Image}") }; var chatRequest = new ChatCompletionOptions(); var chatClient = client.GetChatClient(deployment); var response = await chatClient.CompleteChatAsync(chatMessages, chatRequest); // Stuck here. var content = response.Value.Content; } catch(Exception ex) { throw; } } private static string EncodeImage(string imagePath) { byte[] imageBytes = File.ReadAllBytes(imagePath); return Convert.ToBase64String(imageBytes); } }
Below is the Python implementation that works correctly:
PYTHONfrom openai import AzureOpenAI import base64 endpoint = 'https://xyz.openai.azure.com/' deployment = 'gpt-4o' api_key = "LFK" api_version = "2024-05-01-preview" def encode_image(image_path): """Encodes an image to base64 format.""" with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") def analyze_image(image_path): base64_image = encode_image(image_path) client = AzureOpenAI( azure_endpoint=endpoint, api_key=api_key, api_version=api_version ) response = client.chat.completions.create( model="gpt-4o", messages=[ {"role": "system", "content": "Analyze the uploaded image and return a single-word description of the main subject. The response should be only one word, representing the most general yet accurate category."}, { "role": "user", "content": [ {"type": "text", "text": "What ?"}, {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}} ], } ] ) return response.choices[0].message.content image_path = "image3.png" result = analyze_image(image_path) print("AI Response:", result)
Why is the .NET code not responding as expected?
Any insights or suggestions would be appreciated.
Comments 0
•Answers 2
•Views 56
Answer by TychoAI Agent • 1 month ago
It turns out that the .NET code is “hanging” because its payload isn’t structured the way the GPT‑4o model expects for an image request. In your Python example you’re sending a composite message where the user’s message is a list containing both a text part and an image part (using a dedicated "type": "image_url" element). In the .NET sample you’re just constructing a UserChatMessage with a string that embeds the base‑64 image. That means the service isn’t receiving the correctly formatted image input—and it ends up waiting (or retrying) without error.
Two things to check in your .NET implementation:
Message Format:
The GPT‑4o endpoint expects a composite message for image inputs. In your Python code the user’s message is split into parts—a text portion and an image portion (with type “image_url”). The current ChatMessage types of the .NET library only accept a string for the content, so you likely need to change how you build and send the message payload if the .NET SDK supports such composite messages.
API Version/Configuration:
Your Python client is initialized with the api_version ("2024-05-01-preview"), which might be required for GPT‑4o image support. In the .NET code you’re not specifying an API version. Depending on the SDK version, you might need to pass the correct options (or use an overload) so that the client and its requests are made against the proper preview endpoint.
To move forward, you should verify whether the .NET client library supports sending mixed content (text + image parts) for GPT‑4o. Check the latest documentation for the image input support in the .NET SDK. If it isn’t supported (or is done differently), you may need to either build your own request body or wait for an updated SDK that mirrors the functionality available in the Python library.
In summary, the .NET code isn’t returning a response because it’s sending the image data in an unsupported format (as a plain text string) and may be missing required configuration (like the API version). Adjusting the payload format and ensuring the proper client configuration should resolve the issue.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by NeutronEnvoy476 • 1 month ago
I am able to resolve this issue.
The first thing to correct is that we have to use BinaryData instead of base64 encoded string.
CSHARPbyte[] imageBytes = File.ReadAllBytes(imagePath); var binaryImage = BinaryData.FromBytes(imageBytes);
The next thing is to correctly create UserChatMessage
object to pass image in the chat.
CSHARPvar chatImageContent = ChatMessageContentPart.CreateImagePart(binaryImage, "image/png"); var chatTextContent = ChatMessageContentPart.CreateTextPart("What is in this image?"); var userChatMessage = new UserChatMessage(chatTextContent, chatImageContent); var chatMessages = new List<ChatMessage> { new SystemChatMessage("Analyze the uploaded image."), new UserChatMessage(chatTextContent, chatImageContent) }; var chatClient = client.GetChatClient(deployment); var response = await chatClient.CompleteChatAsync(chatMessages, chatRequest); var content = response.Value.Content[0].Text;
No comments yet.
No comments yet.