Implementation:Microsoft Semantic kernel HuggingFace TextGeneration TestData
| Knowledge Sources | |
|---|---|
| Domains | HuggingFace, Text_Generation, Unit_Testing, Test_Data |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Concrete mock HuggingFace text generation API response JSON file used in unit tests, provided by the Connectors.HuggingFace.UnitTests project.
Description
textgeneration_test_response.json is a test fixture file containing a mock response from the HuggingFace Inference API for the text generation task. At 913 lines, the file consists of a JSON array with a single object containing:
- generated_text -- A string with the generated text continuation (in this case, an essay about the difference between Data Science and AI Engineering, generated for 150 tokens).
- details -- A metadata object containing:
- finish_reason -- "length" (indicating generation stopped at the token limit)
- generated_tokens -- 150 (the number of tokens produced)
- seed -- null (no seed was specified)
- prefill -- An empty array (no prefill tokens)
- tokens -- A large array of token objects, each containing id (integer token ID), text (string token text), logprob (float log probability), and special (boolean indicating if this is a special token)
The tokens array makes up the bulk of the file (913 lines), providing detailed per-token metadata that enables unit tests to verify the connector's token-level parsing capabilities.
Usage
This file is loaded at test time through the HuggingFaceTestHelper.GetTestResponse() helper method. It is used by HuggingFaceTextGenerationTests to mock HTTP responses for text generation endpoint testing. Developers modify this file when the HuggingFace text generation API response format changes or when new token-level test scenarios are needed.
Code Reference
Source Location
- Repository: Microsoft_Semantic_kernel
- File: dotnet/src/Connectors/Connectors.HuggingFace.UnitTests/TestData/textgeneration_test_response.json
- Lines: 1-913
Signature
[
{
"generated_text": "Write about the difference between Data Science and AI Engineering.\n\nData Science and AI Engineering are two interconnected fields...",
"details": {
"finish_reason": "length",
"generated_tokens": 150,
"seed": null,
"prefill": [],
"tokens": [
{
"id": 13,
"text": "\n",
"logprob": -0.11578369,
"special": false
},
{
"id": 13,
"text": "\n",
"logprob": -0.15930176,
"special": false
},
{
"id": 1333,
"text": "Data",
"logprob": -0.25341797,
"special": false
}
]
}
}
]
Import
// Via the HuggingFaceTestHelper utility class:
using SemanticKernel.Connectors.HuggingFace.UnitTests;
// In test methods:
string responseJson = HuggingFaceTestHelper.GetTestResponse("textgeneration_test_response.json");
// Used to populate mock HTTP response:
this._messageHandlerStub.ResponseToReturn.Content =
new StringContent(responseJson);
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| N/A (static file) | JSON file | N/A | This is a static test fixture; it has no runtime inputs |
Outputs
| Name | Type | Description |
|---|---|---|
| [].generated_text | string | The full generated text continuation from the model |
| [].details.finish_reason | string | Reason generation stopped ("length", "eos_token", or "stop_sequence") |
| [].details.generated_tokens | integer | Total number of tokens generated |
| [].details.seed | integer or null | Random seed used for generation, null if not specified |
| [].details.prefill | array | Array of prefill tokens (empty in this test data) |
| [].details.tokens | array | Array of token objects with id, text, logprob, and special fields |
| [].details.tokens[].id | integer | Token ID from the model's vocabulary |
| [].details.tokens[].text | string | Decoded text representation of the token |
| [].details.tokens[].logprob | float | Log probability of the token |
| [].details.tokens[].special | boolean | Whether this is a special token (BOS, EOS, PAD, etc.) |
Usage Examples
Loading in HuggingFace Text Generation Tests
using System.Net.Http;
using Xunit;
public class HuggingFaceTextGenerationTests
{
[Fact]
public async Task ItReturnsTextGenerationCorrectly()
{
// Arrange
this._messageHandlerStub.ResponseToReturn.Content =
new StringContent(
HuggingFaceTestHelper.GetTestResponse(
"textgeneration_test_response.json"));
// Act
var results = await this._textGeneration
.GetTextContentsAsync("Write about the difference between Data Science and AI Engineering.");
// Assert
Assert.NotNull(results);
Assert.Single(results);
Assert.Contains("Data Science", results[0].Text);
}
}
Related Pages
- Environment:Microsoft_Semantic_kernel_DotNet_SDK_Environment
- Microsoft_Semantic_kernel_HuggingFace_Embeddings_TestData -- Mock HuggingFace embeddings response in the same test project
- Microsoft_Semantic_kernel_Google_Embeddings_TestData -- Mock Google AI API test data (different service, similar testing pattern)
- Microsoft_Semantic_kernel_MistralAI_Embeddings_TestData -- Mock MistralAI API test data (different service, similar testing pattern)