Add token stats and raw completion info; prevent jailbreaking - #336
Open
robgruen wants to merge 4 commits into
Open
Add token stats and raw completion info; prevent jailbreaking#336robgruen wants to merge 4 commits into
robgruen wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Copilot review overview
Review tier: Lite
Findings: 1
New issues introduced by this change (2)
| Severity | Finding |
|---|---|
src/typechat/LanguageModel.cs — When OpenAIConfig.UseResponsesApi is explicitly set to false, non-Azure configuration should ensure… |
|
src/typechat/HttpEx.cs — GetJsonResponseWithRawAsync reuses a single StringContent across retries, but… |
What changed in this PR
This PR upgrades TypeChat’s language-model abstraction to return a richer LanguageModelResponse (text + normalized completion metadata), adds normalized token/finish-reason types, updates prompts to JSON-encode user/error sections to reduce prompt-injection risk, and expands unit tests to cover the new behaviors (including OpenAI “Responses API” routing).
Changes:
- Changed
ILanguageModel.CompleteAsync(and implementations) to returnLanguageModelResponse, carryingCompletionInfo(model, finish reason, token usage, raw JSON). - Added non-throwing
JsonTranslator.TranslateToResultAsyncAPIs returningResult<T>with attached completion metadata on success. - Added HTTP helper to capture raw JSON responses and updated prompt templates to JSON-string encode untrusted request/error text.
| File | Description |
|---|---|
| tests/TypeChat.UnitTests/TestLanguageModel.cs | Adds coverage for CompletionInfo parsing and /responses endpoint routing. |
| tests/TypeChat.UnitTests/TestJsonTranslatorPrompts.cs | Verifies prompt sections JSON-encode untrusted request/error text. |
| tests/TypeChat.TestLib/Models.cs | Updates mock language model to new LanguageModelResponse return type. |
| tests/TypeChat.IntegrationTests/TestEndToEnd.cs | Adjusts assertion to handle LanguageModelResponse (string conversion). |
| src/typechat/TokenUsage.cs | Introduces normalized token usage model. |
| src/typechat/Result.cs | Adds optional CompletionInfo to successful results. |
| src/typechat/OpenAIConfig.cs | Adds UseResponsesApi selector for endpoint variant selection. |
| src/typechat/LanguageModelResponse.cs | New response wrapper with implicit conversion to/from string. |
| src/typechat/LanguageModel.cs | Adds Responses API support, endpoint inference/rewrites, and completion metadata extraction. |
| src/typechat/JsonTranslatorPrompts.cs | Switches prompt embedding to JSON-string encoding for safety/consistency. |
| src/typechat/JsonTranslator.cs | Adds non-throwing translation APIs and propagates completion info + repair attempts. |
| src/typechat/ILanguageModel.cs | Interface updated to return LanguageModelResponse. |
| src/typechat/HttpEx.cs | Adds GetJsonResponseWithRawAsync to return parsed + raw response JSON. |
| src/typechat/CompletionInfo.cs | New normalized completion metadata container (model/usage/finish/raw/repairs). |
| src/typechat/CompletionFinishReason.cs | New normalized finish reason enum. |
| src/typechat.sk/TextCompletionModel.cs | Updates SK adapter to return LanguageModelResponse. |
| src/typechat.sk/ChatLanguageModel.cs | Updates SK adapter to return LanguageModelResponse. |
| src/typechat.meai/ChatLanguageModel.cs | Updates MEAI adapter to return LanguageModelResponse with normalized metadata. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


This pull request introduces enhanced completion metadata and improves error handling and telemetry for language model completions. The main change is that all
CompleteAsyncmethods now return aLanguageModelResponseobject, which includes not only the completion text but also normalized metadata such as model name, token usage, finish reason, repair attempts, and the raw API response. TheJsonTranslatornow exposes non-throwing translation methods that return aResult<T>with this metadata, and error handling is improved to return detailed failure information instead of throwing exceptions. Additional supporting types and utilities are added to enable these features.API and Interface Changes:
ILanguageModel.CompleteAsyncimplementations and interface now return aLanguageModelResponse(instead of just a string), carrying both the completion text and metadata. [1] [2] [3] [4] [5]CompletionInfo(metadata about completions) andCompletionFinishReason(normalized stop reason), and updated the pipeline to populate them. [1] [2]JsonTranslator Improvements:
TranslateToResultAsync) that return aResult<T>with completion metadata and do not throw on failure, making error handling more robust and telemetry easier. The old throwing overload is marked for deprecation. [1] [2]HTTP and Model Response Handling:
GetJsonResponseWithRawAsyncto return both parsed and raw JSON from the API, enabling the raw response to be surfaced in telemetry.LanguageModelto use this new HTTP utility and to support both Chat Completions and Responses APIs, with correct endpoint selection and request formatting. [1] [2] [3] [4]Prompt and Repair Prompt Updates:
These changes provide a more robust, extensible, and observable language model integration, making it easier to track usage, handle errors, and support multiple completion APIs.