Skip to content

Add token stats and raw completion info; prevent jailbreaking - #336

Open
robgruen wants to merge 4 commits into
mainfrom
dev/robgruen/jailbreak_fix
Open

Add token stats and raw completion info; prevent jailbreaking#336
robgruen wants to merge 4 commits into
mainfrom
dev/robgruen/jailbreak_fix

Conversation

@robgruen

Copy link
Copy Markdown
Contributor

This pull request introduces enhanced completion metadata and improves error handling and telemetry for language model completions. The main change is that all CompleteAsync methods now return a LanguageModelResponse object, 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. The JsonTranslator now exposes non-throwing translation methods that return a Result<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:

  • All ILanguageModel.CompleteAsync implementations and interface now return a LanguageModelResponse (instead of just a string), carrying both the completion text and metadata. [1] [2] [3] [4] [5]
  • Introduced new types: CompletionInfo (metadata about completions) and CompletionFinishReason (normalized stop reason), and updated the pipeline to populate them. [1] [2]

JsonTranslator Improvements:

  • Added new methods (TranslateToResultAsync) that return a Result<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]
  • The translation workflow now tracks and records the number of repair attempts and attaches completion info to the result. [1] [2] [3] [4]

HTTP and Model Response Handling:

  • Added GetJsonResponseWithRawAsync to return both parsed and raw JSON from the API, enabling the raw response to be surfaced in telemetry.
  • Updated LanguageModel to 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:

  • The prompt sections for user requests and repair instructions are now encoded as JSON strings for improved consistency and safety.

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.

@robgruen
robgruen requested a lite review from Copilot August 28, 2026 22:58
@robgruen
robgruen requested a review from a team as a code owner August 28, 2026 22:58

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

Review tier: Lite
Findings: 1 High severity · 1 Medium severity

New issues introduced by this change (2)
Severity Finding
High severity src/​typechat/​LanguageModel.cs — When OpenAIConfig.UseResponsesApi is explicitly set to false, non-Azure configuration should ensure…
Medium severity 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 return LanguageModelResponse, carrying CompletionInfo (model, finish reason, token usage, raw JSON).
  • Added non-throwing JsonTranslator.TranslateToResultAsync APIs returning Result<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.

Comment thread src/typechat/LanguageModel.cs Outdated
Comment thread src/typechat/HttpEx.cs Outdated
robgruen and others added 2 commits August 28, 2026 16:04
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants